Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling unsuccessful websocket upgrade requests in Javascript Client

I want to have a javascript client process get the HTTP status code that a server is returning when the client makes a websocket upgrade request and that request is unsuccessful.

I have my server returning HTTP 400 to indicate that a websocket upgrade is unsuccessful.

I am using Google Chrome and when i open the developer console I can see the following message:

WebSocket connection to 'wss://' failed: Error during WebSocket handshake: Unexpected response code: 400

However, the onerror handler does not contain this message, it receives a 1006 error but does not indicate that the closure occured as a result of getting HTTP 400.

How does a javascript developer handle handshake errors? I would like to provide the client with an informative message when they get a handshake error.

I have put the websocket error below, it does not seem to contain anything that I can use to indicate that this error is a result of a websocket handshake error.

 Websocket Error: {"path":{"length":0},"cancelBubble":false,"returnValue":true,"srcElement":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"defaultPrevented":false,"timeStamp":1417828938039,"cancelable":false,"bubbles":false,"eventPhase":2,"currentTarget":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"target":{"binaryType":"blob","protocol":"","extensions":"","bufferedAmount":0,"readyState":3,"url":"wss://<my address>","URL":"wss://<my address>"},"type":"error"}
like image 312
Benjamin Weber Avatar asked Dec 06 '14 01:12

Benjamin Weber


People also ask

Can a client have multiple WebSocket connections?

A server can open WebSocket connections with multiple clients—even multiple connections with the same client. It can then message one, some, or all of these clients. Practically, this means multiple people can connect to our chat app, and we can message some of them at a time.

What causes a WebSocket to fail?

The error event is fired when a connection with a WebSocket has been closed due to an error (some data couldn't be sent for example).

When using WebSocket send () How do you know when the data sent has been transmitted to the network?

The only way to know the client received the webSocket message for sure is to have the client send your own custom message back to the server to indicate you received it and for you to wait for that message on the server. That's the ONLY end-to-end test that is guaranteed.


1 Answers

I am afraid there is no way from Javascript to know the HTTP status code of the negotiation.

There are defined closing codes, and 1006 only means that the connection is closed abruptly, but the protocol even allows to close the connection without providing a reason. That, together with the readyState API, are the only tools you have to diagnosed the reason of the problem.

like image 152
vtortola Avatar answered Oct 29 '22 17:10

vtortola