Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to get the reason for a close in websocket?


Asked by Nathanael Parra on Dec 14, 2021 FAQ



A WebSocketSharp.CloseEventArgs instance is passed as e. If you would like to get the reason for the close, you should access e.Code or e.Reason property. e.Code property returns a ushort that represents the status code for the close.
Additionally,
Here is how to inform the user that the network is not available and try to reconnect when a WebSocket close event occurs − socket.onclose = function (event) { // Connection closed. // Firstly, check the reason. if (event.code != 1000) { // Error code 1000 means that the connection was closed normally.
Besides, Normally, when a party wants to close the connection (both browser and server have equal rights), they send a “connection close frame” with a numeric code and a textual reason. The method for that is: socket.close([ code], [ reason]); code is a special WebSocket closing code (optional)
Similarly,
If any header is not understood or has an incorrect value, the server should send a 400 ("Bad Request")} response and immediately close the socket. As usual, it may also give the reason why the handshake failed in the HTTP response body, but the message may never be displayed (browsers do not display it).
In this manner,
Any other way to tell different closing reasons apart? Close Code 1006 is a special code that means the connection was closed abnormally (locally) by the browser implementation. If your browser client reports close code 1006, then you should be looking at the websocket.onerror (evt) event for details.