Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch `WebSocket connection to 'ws://xxx:nn' failed: Connection closed before receiving a handshake response` error?

Let's say I have a node server started with port nn, and there is not any WebSocket service on it.

And the problem is that my client trys to connect a WebSocket service to this server.

Obviously, it should fail to connect to the server.

But I couldn't catch this error, my client code is the following:

try {     this.ws = new WebSocket('ws://xxx:nn'); } catch ( e ) {     fallback();     console.warn(e); } 

My expectation is that the fallback method gets called while connecting fails, but actually the error is not caught by the above try...catch

Does anyone know how to catch the error in my usecase?

like image 454
Howard Avatar asked Sep 11 '14 05:09

Howard


People also ask

When WebSocket connection is closed?

The WebSocket is closed before the connection is established error message indicates that some client code, or other mechanism, has closed the websocket connection before the connection was fully established.

How do I check my WebSocket connection status?

In the search field, enter websocket . From the search results, click WebSocket Connection Status.

Can I reopen a closed WebSocket?

Just make a new WebSocket again. The reason a reconnect doesn't exist is because it would probably be just like creating a new one. Possible duplicate of Reconnection of client when the server reboots in WebSocket.

What causes error code 307 during WebSocket handshake?

This error is usually caused by a client using only the WebSockets transport but the WebSockets protocol is not enabled on the server. WebSocket connection to 'ws://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 307

What is WebSockets handling errors?

WebSockets - Handling Errors. Once a connection has been established between the client and the server, an open event is fired from the Web Socket instance. Error are generated for mistakes, which take place during the communication. It is marked with the help of onerror event.

Why did WebSocket connection to'WSS://XXX/hubname'fail?

WebSocket connection to 'wss://xxx/HubName' failed: Error during WebSocket handshake: Unexpected response code: 404 When using multiple servers without sticky sessions, the connection can start on one server and then switch to another server. The other server is not aware of the previous connection.

What is onerror event in web socket?

Once a connection has been established between the client and the server, an open event is fired from the Web Socket instance. Error are generated for mistakes, which take place during the communication. It is marked with the help of onerror event. Onerror is always followed by termination of connection.


2 Answers

You can register for onError callback of websocket object

exampleSocket.onerror=function(event){     console.log("Error"); } 

You can refer following example. http://jsfiddle.net/g28yuymv/1/

catching error example http://jsfiddle.net/g28yuymv/4/

like image 154
shivakumar Avatar answered Sep 29 '22 04:09

shivakumar


Looking at the HTML5 WebSockets spec, you can pass a value into the close() method. Then on the onclose() event listener you can check against that value. This gives you the opportunity to set handlers for different disconnect scenarios and then handle everything else as a generic error.

https://developer.mozilla.org/en-US/docs/Web/API/WebSocket#close()

var _websocket;  function wsConnect() {   if (_websocket) {     _websocket.close(3001);   } else {     _websocket = new WebSocket("wss://echo.websocket.org1");     _websocket.onopen = function() {       console.log('connected');     };     _websocket.onmessage = function(msg) {       console.log(msg);     };      _websocket.onclose = function(evt) {       if (evt.code == 3001) {         console.log('ws closed');         _websocket = null;       } else {         _websocket = null;         console.log('ws connection error');       }     };      _websocket.onerror = function(evt) {       if (_websocket.readyState == 1) {         console.log('ws normal error: ' + evt.type);       }     };   } }  wsConnect(); 

sweet fiddle: https://jsfiddle.net/lamarant/ry0ty52n/

like image 28
lamarant Avatar answered Sep 29 '22 04:09

lamarant