i'm trying use websockets on website, first developed small , simple websocket page test it.
i have websocket server running on localhost, based on python tornado "chat" demo. reason chat demo app runs can't seem use websocket own page although form of connection made.
i testing using latest chromium version, implementing websockets version 13, supported tornado implementation.
so here problem:
- load page, js executes , upgrade request sent server
- server receives request , answers
so here in understanding chrome should set readystate = 1 , should able send messages page.
only reason doesn't work, readystate remains 0 , of course if try send message receive invalid_state_err.
here 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 appreciated :)
--- edit ---
so figured out in end, if run same problem here reason:
websocket's readystate updated when thread ends !
so running code like:
var ws = new websocket(stuff); while(ws.readystate==0){};
will send browser in infinite loop...
running code like:
var ws=new websocket(stuff); do_other_stuf();
might work, wont able use ws.
if code supposed run after socket opens uses socket way have written:
var ws=new websocket(stuff); ws.onopen = new function(){ // code need ws open } // rest of code doesn't require ws open
a better way let thread end using asynchronous call:
var ws = new websocket(stuff); settimeout(whatever,500); function whatever(){ if(ws.readystate==1){ // code want run after ws open } else settimeout(whatever,500); }
in order trigger functionality when websockets connected please use callbacks:
var socket = new websocket(...); socket.onopen = function() { // socket connected }; socket.onerror = function() { // error happened }; socket.onmessage = function(evt) { // message: evt.data };
using readystate
bad thing in such case, if have ask multiple times (is unnecessary).
additionally take in account each vendor of browsers implements websockets slight differences (unfortunately), make sure test in browsers.
Comments
Post a Comment