Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome websockets, readystate always on 0

I'm trying to use websockets on a website, so first I developed a small and very simple websocket page just to test it.

I have a websocket server running on my localhost, it is based on the python Tornado "Chat" demo. For some reason the chat demo app runs perfectly but I can't seem to use the websocket with my own page although some form of connection is made.

I am testing this using the latest Chromium version, so implementing websockets version 13, which is supported by the Tornado implementation.

So here is the problem:

  1. Load page, js executes and Upgrade request is sent to the server
  2. Server receives request and answers

So here in my understanding Chrome should set readyState = 1 and I should be able to send messages from my page.

Only for some reason it doesn't work, readyState remains 0 and of course if I try to send a message I receive an INVALID_STATE_ERR.

Here are the Headers :

Request:

GET ws://127.0.0.1:8000/chatsocket HTTP/1.1
Origin: http://127.0.0.1
Cookie: _xsrf=9f73731fc2d544df864ce777bef0775a
Connection: Upgrade
Host: 127.0.0.1:8000
Sec-WebSocket-Key: pkwlpY+TtxfgUrm3M4WtTQ==
Upgrade: websocket
Sec-WebSocket-Version: 13

Response:

HTTP/1.1 101 Switching Protocols
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Accept: ur9KL2jBhYB38e2SgwOkjyBlQXk=

Any help is appreciated :)



--- EDIT ---


So I figured it out in the end, if you run into the same problem here is the reason:

WebSocket's readyState is updated when the Thread ENDS !

So running a code like:

var ws = new WebSocket(stuff);
while(ws.readyState==0){};

Will send the browser in an infinite loop...

Running code like:

var ws=new WebSocket(stuff);
do_other_stuf();

Might work, but you wont be able to use WS.


If the code that is supposed to run after the socket opens uses the socket this is the way it will have to be written:

var ws=new WebSocket(stuff);
ws.onopen = new function(){
    // some code that need WS to be open
}
// rest of the code that doesn't require WS to be open

A better way to do this would be to let the thread end by using an asynchronous call:

var ws = new WebSocket(stuff);
setTimeout(whatever,500);

function whatever(){
    if(ws.readyState==1){
        // The code you want to run after WS is open
    }
    else
        setTimeout(whatever,500);
}
like image 514
TheC4keIsASpy Avatar asked Mar 21 '13 10:03

TheC4keIsASpy


People also ask

What is readyState in WebSocket?

readyState. The WebSocket. readyState read-only property returns the current state of the WebSocket connection.

Which socket value is readyState?

Socket.readyState A value of 1 indicates that the connection is established and communication is possible. A value of 2 indicates that the connection is going through the closing handshake. A value of 3 indicates that the connection has been closed or could not be opened.

How do I check my WebSocket connection status?

In the search field, enter websocket . From the search results, click WebSocket Connection Status.

What does the value 2 of the WebSocket attribute socket readyState indicate?

What does the value 2 of the WebSocket attribute Socket. readyState indicate? Explanation: The WebSocket object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.


1 Answers

In order to trigger some functionality when WebSockets are connected please use callbacks:

var socket = new WebSocket(...);

socket.onopen = function() {
    // socket is connected
};
socket.onerror = function() {
    // some error happened
};
socket.onmessage = function(evt) {
    // you get a message: evt.data
};

Using readyState is well bad thing to do in such case, especially if you have to ask it multiple times (is just unnecessary).

Additionally take in account that each vendor of browsers implements WebSockets with very slight differences (unfortunately), so make sure you test it in most browsers.

like image 134
moka Avatar answered Oct 06 '22 03:10

moka