javascript - Chrome websockets, readystate always on 0 - Stack Overflow

I'm trying to use websockets on a website, so first I developed a small and very simple websocket

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);
}

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);
}
Share Improve this question edited Mar 21, 2013 at 13:36 TheC4keIsASpy asked Mar 21, 2013 at 10:03 TheC4keIsASpyTheC4keIsASpy 711 gold badge1 silver badge4 bronze badges 1
  • Please, do not use readyState as indication for checking WebSocket connection state. Use only onopen. – moka Commented Jan 14, 2014 at 18:03
Add a ment  | 

1 Answer 1

Reset to default 3

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272026a4566146.html

相关推荐

  • javascript - Chrome websockets, readystate always on 0 - Stack Overflow

    I'm trying to use websockets on a website, so first I developed a small and very simple websocket

    7天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信