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?
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.
In the search field, enter websocket . From the search results, click WebSocket Connection Status.
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.
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
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.
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.
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.
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/
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With