Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to handle Websocket

I've a client to server Websocket connection which should be there for 40 seconds or so. Ideally it should be forever open.

The client continually sends data to server and vice-versa.

Right now I'm using this sequence:

var socket;
function senddata(data)
{

    if (!socket)
    {
        socket = new WebSocket(url);
        socket.onopen = function (evt) {

            socket.send(data);

            socket.onmessage = function (evt) {
                var obj = JSON.parse(evt.data);

                port.postMessage(obj);
            }

            socket.oneerror = function (evt) {
                socket.close();
                socket = null;
            }
           socket.onclose = function(evt){
               socket = null;
           }


        }
    }
    else
    {
        socket.send(data);
    }

}

Clearly as per current logic, in case of error, the current request data may not be sent at all.

To be frank it sometimes gives error that websocket is still in connecting state. This connection breaks often due to networking issues. In short it does not work perfectly well.

I've read a better design : How to wait for a WebSocket's readyState to change but does not cover all cases I need to handle.

Also I've Googled about this but could not get the correct procedure for this.

So what is the right way to send regular data through Websockets which handles well these issues like connection break etc?

like image 201
user5858 Avatar asked Jan 26 '16 10:01

user5858


People also ask

How do you interact with WebSockets?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.

Which of the following correctly create a WebSocket connection?

Following is the API which creates a new WebSocket object. var Socket = new WebSocket(url, [protocal] ); Here first argument, url, specifies the URL to which to connect. The second attribute, protocol is optional, and if present, specifies a sub-protocol that the server must support for the connection to be successful.

What is the protocol for WebSocket?

The WebSocket protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. By default the WebSocket protocol uses port 80 for regular WebSocket connections and port 443 for WebSocket connections tunneled over TLS [RFC2818].

When using WebSocket send () How do you know?

The only way to know the client received the webSocket message for sure is to have the client send your own custom message back to the server to indicate you received it and for you to wait for that message on the server. That's the ONLY end-to-end test that is guaranteed.


1 Answers

An event you don't seem to cover is onclose. Which should work really well, since it's called whenever the connection terminates. This is more reliable than onerror, because not all connection disruptions result in an error.

like image 142
GMchris Avatar answered Oct 06 '22 01:10

GMchris