Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting the reason why websockets closed with close code 1006

I would like to get the reason websockets closed, so I can show the right message to the user.

I have

sok.onerror=function (evt)       {//since there is an error, sockets will close so...        sok.onclose=function(e){            console.log("WebSocket Error: " , e);} 

The code is always 1006 and the reason is always " ". But I want to tell different closing reasons apart.

For example the comand line gives an error reason : "you cannot delete that, because database wont let you". But on Chrome's console, the reason is still " ".

Any other way to tell different closing reasons apart?

like image 657
slevin Avatar asked Oct 10 '13 19:10

slevin


People also ask

What causes a WebSocket to close?

Parameters. An integer WebSocket connection close code value indicating a reason for closure: If unspecified, a close code for the connection is automatically set: to 1000 for a normal closure, or otherwise to another standard value in the range 1001 - 1015 that indicates the actual reason the connection was closed.

How do I get rid of WebSocket error?

Simply go to control panel in your system and select java and uninstall. Then go to google and download latest java 8 update 162 32 bit java. Download Java in install in your system. Restart one your system your issue resolve successfully.

What is WebSocket timeout?

The websocket-idle-timeout command sets the maximum idle time for client connections with the handler. This timer monitors the idle time in the data transfer process. If the specified idle time is exceeded, the connection is torn down.


Video Answer


2 Answers

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.

However, Chrome will rarely report any close code 1006 reasons to the Javascript side. This is likely due to client security rules in the WebSocket spec to prevent abusing WebSocket. (such as using it to scan for open ports on a destination server, or for generating lots of connections for a denial-of-service attack).

Note that Chrome will often report a close code 1006 if there is an error during the HTTP Upgrade to Websocket (this is the step before a WebSocket is technically "connected"). For reasons such as bad authentication or authorization, or bad protocol use (such as requesting a subprotocol, but the server itself doesn't support that same subprotocol), or even an attempt at talking to a server location that isn't a WebSocket (such as attempting to connect to ws://images.google.com/)

Fundamentally, if you see a close code 1006, you have a very low level error with WebSocket itself (similar to "Unable to Open File" or "Socket Error"), not really meant for the user, as it points to a low level issue with your code and implementation. Fix your low level issues, and then when you are connected, you can then include more reasonable error codes. You can accomplish this in terms of scope or severity in your project. Example: info and warning level are part of your project's specific protocol, and don't cause the connection to terminate. With severe or fatal messages reporting also using your project's protocol to convey as much detail as you want, and then closing the connection using the limited abilities of the WebSocket close flow.

Be aware that WebSocket close codes are very strictly defined, and the close reason phrase/message cannot exceed 123 characters in length (this is an intentional WebSocket limitation).

But not all is lost, if you are just wanting this information for debugging reasons, the detail of the closure, and its underlying reason is often reported with a fair amount of detail in Chrome's Javascript console.

like image 191
Joakim Erdfelt Avatar answered Oct 01 '22 03:10

Joakim Erdfelt


In my and possibly @BIOHAZARD case it was nginx proxy timeout. In default it's 60 sec without activity in socket

I changed it to 24h in nginx and it resolved problem

proxy_read_timeout 86400s; proxy_send_timeout 86400s; 
like image 42
mixalbl4 Avatar answered Oct 01 '22 02:10

mixalbl4