javascript - Using new Websocket(<uri>) causes the websocket to hang on connecting even after handshake is received -


i have very irritating bug in code doesn't seem show itself.

the situation: using 3 different websockets connect 3 different websocketservers through localhost. client connects server , on... client cycles through connections make , tries each 1 in order. if fails or succeeds, goes next one. if connection failed, connection make placed @ of queue. switch next 1 in list using both onopen , onclose function.

the problem: using following code snippet start connection client:

this.ws = new websocket(this.url); this.ws.parent = this; this.ws.onopen = this.onopen; this.ws.onmessage = this.onmessage; this.ws.onerror = this.onerror; this.ws.onclose = this.onclose; 

the object wrapper object in case. functions supplied found , correct.

what happens is, connection created server. server sends basic handshake reply. wiresharked situation , 100% sure handshake received client (it ack'ed tcp connection). however, onopen function not called , websocket seems stay in connecting state. if wait long, can timeout on handshake. if close server, error connection has reset.

this problem random. when start client, work , 3 connections made succesfully. sometimes, fails @ first attempt connect first server. first connection successfull , second fails , on.

the problem shows under windows opera, chrome , internet explorer.

i tried without cycling through list of connections make let each socket retry connection on own onclose. doesn't fix either.

can please end suffering? last thing can think of, each javascript engine broken which, of course, unlikely.

one situation used to, more or less, force problem of time (doesn't work everytime) start server , open page in client. causes connection on , b fail , c succeed on first try hangs (most of times).

is possible bug caused fact websocket has failed , next 1 hangs because of reason of previous failed one?

code on failed close try reconnect later:

socketstoconnect.addtoconnect(object.parent); socketstoconnect.trytoconnectnext(); 

code on succesfull open try , connect next:

socketstoconnect.trytoconnectnext(); 

code know connect still (addconnect takes wrapper websocket object):

function socketstoconnect() {     this.toconnect = [];      this.addtoconnect = function(websocket) {         this.toconnect.push(websocket);     }      this.trytoconnectnext = function() {         for(var =0; < this.toconnect.length; i++) {             console.log(this.toconnect[i]);         }          var next = this.toconnect.shift();          if(next) {             next.connect();         }     } } 

global variable:

var socketstoconnect = new socketstoconnect(); 

wrapper websocket object:

function websocket(name, ipaddress, port, onopen, onmessage, onerror, onclose) {     this.name = name;     this.ipaddress = ipaddress;     this.port = port;     this.url = "ws://"+ipaddress+":"+port+"/";     this.connected = false;     this.onopen = onopen;     this.onmessage = onmessage;     this.onerror = onerror;     this.onclose = onclose;      this.connect = function() {                                 this.ws = new websocket(this.url);                                 this.ws.parent = this;                                 this.ws.onopen = this.onopen;                                 this.ws.onmessage = this.onmessage;                                 this.ws.onerror = this.onerror;                                 this.ws.onclose = this.onclose;         }      this.sendmessage = function (message) {                            console.log("sending on ws[" + this.name + "]: " + message);                            this.ws.send(message);                        };      this.sendjsonobject = function (jsonobject) {                             this.sendmessage(json.stringify(jsonobject));                           };      this.close = function() {                     console.log("closing websocket[" + this.name + "]");                     this.ws.close();                  }; } 

edit: websocket handshake wiresharked:

get / http/1.1 host: 127.0.0.1:9163 connection: upgrade pragma: no-cache cache-control: no-cache upgrade: websocket origin: null sec-websocket-version: 13 user-agent: mozilla/5.0 (windows nt 6.3; wow64) applewebkit/537.36 (khtml, gecko) chrome/42.0.2311.135 safari/537.36 accept-encoding: gzip, deflate, sdch accept-language: nl-nl,nl;q=0.8,en-us;q=0.6,en;q=0.4 sec-websocket-key: u0wpfxitd+tqaljgsyxeyw== sec-websocket-extensions: permessage-deflate; client_max_window_bits   http/1.1 101 websocket protocol handshake upgrade: websocket connection: upgrade sec-websocket-accept: fwqyzzxhrtdfdwmwlhea5d52jim=\r\n \r\n 

edit2: found post on superusers has same symptoms different cause. https://superuser.com/questions/765076/websocket-handshake-response-not-forwarded-from-tcp-to-client checked sha1 accept value , checks out.

edit3: had @ calls using chrome developertools. kinda weird , may because not understand happening. when have @ timeline, can see function calls in order. everytime line new websocket(this.url); called, chrome will, on successfull connection steps [destroy websocket, send websocket handshake, receive websocket handsake]. when stalls on connecting state, chrome shows step [destroy websocket] , seems "send websocket handshake" event gets lost. sounds plausible indeed cause underlying code send handshake request, never try read response. seems show might javascript bug.

could please show thoughts?

edit 5: here picture of timeline in google chrome talked in "edit 4". line 38 new websocket(this.url); line , order in connections happen canvas.js, keyboard.js , mouse.js(never comes far). canvas.js succeeds , keyboard.js stalls in connecting state. screenshot while keyboard.js hangs. http://i61.tinypic.com/6j3n9d.png


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -