From 9479c72083031d137d694c6eedc14d8fd8c053da Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 1 Jul 2010 10:32:14 -0500 Subject: [PATCH] web-socket-js event fixes. When using web-socket-js, the onopen event may happen inline so the caller may not have time to set onopen before the event fires. In this case set a short timeout and try again. In particular this affects Opera most of the time. Also, to get around Opera event droppings, always read the readyState directly instead of relying on the local readyState variable to be correct (which it isn't if stateChange event were dropped). --- include/web-socket-js/web_socket.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/include/web-socket-js/web_socket.js b/include/web-socket-js/web_socket.js index beaa40f0..6be794f8 100755 --- a/include/web-socket-js/web_socket.js +++ b/include/web-socket-js/web_socket.js @@ -36,8 +36,16 @@ WebSocket.__flash.create(url, protocol, proxyHost || null, proxyPort || 0, headers || null); self.__flash.addEventListener("open", function(fe) { + console.log("web-socket.js open"); try { - if (self.onopen) self.onopen(); + if (self.onopen) { + self.onopen(); + } else { + // If "open" comes back in the same thread, then the + // caller will not have set the onopen handler yet. + setTimeout(function () { + if (self.onopen) { self.onopen(); } }, 10); + } } catch (e) { console.error(e.toString()); } @@ -95,6 +103,7 @@ } WebSocket.prototype.send = function(data) { + this.readyState = this.__flash.getReadyState(); if (!this.__flash || this.readyState == WebSocket.CONNECTING) { throw "INVALID_STATE_ERR: Web Socket connection has not been established"; } @@ -109,6 +118,7 @@ WebSocket.prototype.close = function() { if (!this.__flash) return; + this.readyState = this.__flash.getReadyState(); if (this.readyState != WebSocket.OPEN) return; this.__flash.close(); // Sets/calls them manually here because Flash WebSocketConnection.close cannot fire events