Chrome websockets, readystate always on 0
chromium, google-chrome, html, javascript, websocket
Solution
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.
Problem
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: - Load page, js executes and Upgrade request is sent to the server - 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); } ```