How to avoid the error.code 1006 in WebSocket

2

On the client side, I have websocket configured with javascript. On the server side, I have websocket configured with java.

The configuration is quite simple and works correctly. Once a day, randomly, the clients (javascript) begin to give the error code 1006 and disconnect. For the moment, what I did was detect error 1006 and reconnect the websocket to the server in the following way:

socket.onclose = function (msg) {
  if (msg.code == "1006") {
    setTimeout(function () {
      conectar(); => funcion que reconecta el websocket
    }, 1000)
  }
};

What I want to avoid is error 1006, since it disconnects all clients at the same time. Looking for a bit, I find the following:

1006

  1006 is a reserved value and MUST NOT be set as a status code in a
  Close control frame by an endpoint.  It is designated for use in
  applications expecting a status code to indicate that the
  connection was closed abnormally, e.g., without sending or
  receiving a Close control frame.

Even looking for a bit more:

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).

Although it is clear to me that the error comes from the lack of sending or receiving a control frame, it is not clear to me how to solve it and if there is a possibility to do so.

Did someone have a similar problem and solve it correctly?

    
asked by Martin G. 04.04.2017 в 21:32
source

0 answers