Commit 9479c720 authored by Joel Martin's avatar Joel Martin

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).
parent 67134184
...@@ -36,8 +36,16 @@ ...@@ -36,8 +36,16 @@
WebSocket.__flash.create(url, protocol, proxyHost || null, proxyPort || 0, headers || null); WebSocket.__flash.create(url, protocol, proxyHost || null, proxyPort || 0, headers || null);
self.__flash.addEventListener("open", function(fe) { self.__flash.addEventListener("open", function(fe) {
console.log("web-socket.js open");
try { 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) { } catch (e) {
console.error(e.toString()); console.error(e.toString());
} }
...@@ -95,6 +103,7 @@ ...@@ -95,6 +103,7 @@
} }
WebSocket.prototype.send = function(data) { WebSocket.prototype.send = function(data) {
this.readyState = this.__flash.getReadyState();
if (!this.__flash || this.readyState == WebSocket.CONNECTING) { if (!this.__flash || this.readyState == WebSocket.CONNECTING) {
throw "INVALID_STATE_ERR: Web Socket connection has not been established"; throw "INVALID_STATE_ERR: Web Socket connection has not been established";
} }
...@@ -109,6 +118,7 @@ ...@@ -109,6 +118,7 @@
WebSocket.prototype.close = function() { WebSocket.prototype.close = function() {
if (!this.__flash) return; if (!this.__flash) return;
this.readyState = this.__flash.getReadyState();
if (this.readyState != WebSocket.OPEN) return; if (this.readyState != WebSocket.OPEN) return;
this.__flash.close(); this.__flash.close();
// Sets/calls them manually here because Flash WebSocketConnection.close cannot fire events // Sets/calls them manually here because Flash WebSocketConnection.close cannot fire events
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment