Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: unexpected server response (404) #1208

Closed
dennybritz opened this issue Sep 27, 2017 · 5 comments
Closed

Error: unexpected server response (404) #1208

dennybritz opened this issue Sep 27, 2017 · 5 comments

Comments

@dennybritz
Copy link

I am trying to connect to PubNub using a websocket, just like the example here but with node: https://www.pubnub.com/websockets/

The example code works fine in the browser, but using ws I get:

Error: unexpected server response (404)
    at ClientRequest.response (/Users/dennybritz/node_modules/ws/lib/WebSocket.js:719:15)
    at Object.onceWrapper (events.js:316:30)
    at emitOne (events.js:115:13)
    at ClientRequest.emit (events.js:210:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:565:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
    at TLSSocket.socketOnData (_http_client.js:454:20)
    at emitOne (events.js:115:13)
    at TLSSocket.emit (events.js:210:7)
    at addChunk (_stream_readable.js:266:12)

Any idea what could be going wrong here? Any workaround?

@dennybritz
Copy link
Author

Can be closed, apparently they're having their own websocket implementation which is just a wrapper.

@paulddrix
Copy link

I search for other issues but this was the one that was most similar to mine. if not please point me in the right direction, thank you.

I am trying to build tests for my api and when I send error code and messages from the api to the client in the verifyClient function like so:
websocket server code:

// return error message
done(false, 401, 'Invalid or missing parameters.');

However, when I go to check the error message it does not appear, the library only returns the statusCode from the api response. Is there any way to retrieve the reason phrase that was sent.

client code:

/* Error Event Handler */
   ws.on('error',  (e) => {
     // need to get both the statusCode and the reason phrase
     console.log(e);
     // assert the status code and reason phrase
     done();
   });

node_modules/ws/lib/WebSocket.js:653

this._req.on('response', (res) => {
    if (!this.emit('unexpected-response', this._req, res)) {
      this._req.abort();
      this.finalize(new Error(`unexpected server response (${res.statusCode})`));
    }
  });

@lpinca
Copy link
Member

lpinca commented Feb 20, 2018

@paulddrix see

ws/test/websocket.test.js

Lines 513 to 526 in 84fa837

ws.on('unexpected-response', (req, res) => {
assert.strictEqual(res.statusCode, 401);
let data = '';
res.on('data', (v) => {
data += v;
});
res.on('end', () => {
assert.strictEqual(data, 'foo');
done();
});
});

@paulddrix
Copy link

My understanding is if a response is received then the unexpected-response event is triggered, that would mean that if the connection is accepted then there should be no response because the request or connection was upgraded, is that correct. if is is then all status codes that are returned can triggered the event. Thank your for your time.

@lpinca
Copy link
Member

lpinca commented Feb 21, 2018

Yes that's correct because for an upgrade request, you should receive an upgrade response.

ws/lib/websocket-server.js

Lines 257 to 262 in 84fa837

const headers = [
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
`Sec-WebSocket-Accept: ${key}`
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants