Update gimite/web-socket-js build.
Update to a build based on 20f837425d4 from gimite/web-socket-js. This changes the event handling code and fixes the frequent recursive call into Flash errors.
This commit is contained in:
parent
1b097a63a3
commit
24d01a5aa4
|
@ -362,7 +362,7 @@ FABridge.prototype =
|
||||||
// accepts a type structure, returns a constructed type
|
// accepts a type structure, returns a constructed type
|
||||||
addTypeDataToCache: function(typeData)
|
addTypeDataToCache: function(typeData)
|
||||||
{
|
{
|
||||||
newType = new ASProxy(this, typeData.name);
|
var newType = new ASProxy(this, typeData.name);
|
||||||
var accessors = typeData.accessors;
|
var accessors = typeData.accessors;
|
||||||
for (var i = 0; i < accessors.length; i++)
|
for (var i = 0; i < accessors.length; i++)
|
||||||
{
|
{
|
||||||
|
|
Binary file not shown.
|
@ -8,7 +8,9 @@
|
||||||
if (window.WebSocket) return;
|
if (window.WebSocket) return;
|
||||||
|
|
||||||
var console = window.console;
|
var console = window.console;
|
||||||
if (!console) console = {log: function(){ }, error: function(){ }};
|
if (!console || !console.log || !console.error) {
|
||||||
|
console = {log: function(){ }, error: function(){ }};
|
||||||
|
}
|
||||||
|
|
||||||
if (!swfobject.hasFlashPlayerVersion("9.0.0")) {
|
if (!swfobject.hasFlashPlayerVersion("9.0.0")) {
|
||||||
console.error("Flash Player is not installed.");
|
console.error("Flash Player is not installed.");
|
||||||
|
@ -31,73 +33,22 @@
|
||||||
WebSocket.__addTask(function() {
|
WebSocket.__addTask(function() {
|
||||||
self.__createFlash(url, protocol, proxyHost, proxyPort, headers);
|
self.__createFlash(url, protocol, proxyHost, proxyPort, headers);
|
||||||
});
|
});
|
||||||
}, 1);
|
}, 0);
|
||||||
}
|
};
|
||||||
|
|
||||||
WebSocket.prototype.__createFlash = function(url, protocol, proxyHost, proxyPort, headers) {
|
WebSocket.prototype.__createFlash = function(url, protocol, proxyHost, proxyPort, headers) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.__flash =
|
self.__flash =
|
||||||
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("event", function(fe) {
|
||||||
self.__flash.addEventListener("open", function(fe) {
|
// Uses setTimeout() to workaround the error:
|
||||||
try {
|
// > You are trying to call recursively into the Flash Player which is not allowed.
|
||||||
self.readyState = self.__flash.getReadyState();
|
setTimeout(function() { self.__handleEvents(); }, 0);
|
||||||
if (self.__timer) clearInterval(self.__timer);
|
|
||||||
if (window.opera) {
|
|
||||||
// Workaround for weird behavior of Opera which sometimes drops events.
|
|
||||||
self.__timer = setInterval(function () {
|
|
||||||
self.__handleMessages();
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
if (self.onopen) self.onopen();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e.toString());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.__flash.addEventListener("close", function(fe) {
|
|
||||||
try {
|
|
||||||
self.readyState = self.__flash.getReadyState();
|
|
||||||
if (self.__timer) clearInterval(self.__timer);
|
|
||||||
if (self.onclose) self.onclose();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.__flash.addEventListener("message", function() {
|
|
||||||
try {
|
|
||||||
self.__handleMessages();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.__flash.addEventListener("error", function(fe) {
|
|
||||||
try {
|
|
||||||
if (self.__timer) clearInterval(self.__timer);
|
|
||||||
if (self.onerror) self.onerror();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
self.__flash.addEventListener("stateChange", function(fe) {
|
|
||||||
try {
|
|
||||||
self.readyState = self.__flash.getReadyState();
|
|
||||||
self.bufferedAmount = fe.getBufferedAmount();
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e.toString());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//console.log("[WebSocket] Flash object is ready");
|
//console.log("[WebSocket] Flash object is ready");
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocket.prototype.send = function(data) {
|
WebSocket.prototype.send = function(data) {
|
||||||
if (this.__flash) {
|
|
||||||
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";
|
||||||
}
|
}
|
||||||
|
@ -111,29 +62,26 @@
|
||||||
if (result < 0) { // success
|
if (result < 0) { // success
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
this.bufferedAmount = result;
|
this.bufferedAmount += result;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocket.prototype.close = function() {
|
WebSocket.prototype.close = function() {
|
||||||
if (!this.__flash) return;
|
var self = this;
|
||||||
this.readyState = this.__flash.getReadyState();
|
if (!self.__flash) return;
|
||||||
if ((this.readyState === WebSocket.CLOSED) ||
|
if (self.readyState == WebSocket.CLOSED || self.readyState == WebSocket.CLOSING) return;
|
||||||
(this.readState === WebSocket.CLOSING)) {
|
self.__flash.close();
|
||||||
return;
|
|
||||||
}
|
|
||||||
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
|
||||||
// which causes weird error:
|
// which causes weird error:
|
||||||
// > You are trying to call recursively into the Flash Player which is not allowed.
|
// > You are trying to call recursively into the Flash Player which is not allowed.
|
||||||
this.readyState = WebSocket.CLOSED;
|
self.readyState = WebSocket.CLOSED;
|
||||||
if (this.__timer) clearInterval(this.__timer);
|
if (self.__timer) clearInterval(self.__timer);
|
||||||
if (this.onclose) {
|
if (self.onclose) {
|
||||||
// Make it asynchronous so that it looks more like an actual
|
// Make it asynchronous so that it looks more like an actual
|
||||||
// close event
|
// close event
|
||||||
setTimeout(this.onclose, 1);
|
setTimeout(self.onclose, 0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -201,23 +149,55 @@
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
WebSocket.prototype.__handleMessages = function() {
|
WebSocket.prototype.__handleEvents = function() {
|
||||||
// Gets data using readSocketData() instead of getting it from event object
|
// Gets events using receiveEvents() instead of getting it from event object
|
||||||
// of Flash event. This is to make sure to keep message order.
|
// of Flash event. This is to make sure to keep message order.
|
||||||
// It seems sometimes Flash events don't arrive in the same order as they are sent.
|
// It seems sometimes Flash events don't arrive in the same order as they are sent.
|
||||||
var arr = this.__flash.readSocketData();
|
var events = this.__flash.receiveEvents();
|
||||||
for (var i = 0; i < arr.length; i++) {
|
for (var i = 0; i < events.length; i++) {
|
||||||
var data = decodeURIComponent(arr[i]);
|
|
||||||
try {
|
try {
|
||||||
if (this.onmessage) {
|
var event = events[i];
|
||||||
var e;
|
if ("readyState" in event) {
|
||||||
if (window.MessageEvent) {
|
this.readyState = event.readyState;
|
||||||
e = document.createEvent("MessageEvent");
|
}
|
||||||
e.initMessageEvent("message", false, false, data, null, null, window, null);
|
if (event.type == "open") {
|
||||||
} else { // IE
|
|
||||||
e = {data: data};
|
if (this.__timer) clearInterval(this.__timer);
|
||||||
|
if (window.opera) {
|
||||||
|
// Workaround for weird behavior of Opera which sometimes drops events.
|
||||||
|
this.__timer = setInterval(function () {
|
||||||
|
this.__handleEvents();
|
||||||
|
}, 500);
|
||||||
}
|
}
|
||||||
this.onmessage(e);
|
if (this.onopen) this.onopen();
|
||||||
|
|
||||||
|
} else if (event.type == "close") {
|
||||||
|
|
||||||
|
if (this.__timer) clearInterval(this.__timer);
|
||||||
|
if (this.onclose) this.onclose();
|
||||||
|
|
||||||
|
} else if (event.type == "message") {
|
||||||
|
|
||||||
|
if (this.onmessage) {
|
||||||
|
var data = decodeURIComponent(event.data);
|
||||||
|
var e;
|
||||||
|
if (window.MessageEvent && !window.opera) {
|
||||||
|
e = document.createEvent("MessageEvent");
|
||||||
|
e.initMessageEvent("message", false, false, data, null, null, window, null);
|
||||||
|
} else {
|
||||||
|
// IE and Opera, the latter one truncates the data parameter after any 0x00 bytes.
|
||||||
|
e = {data: data};
|
||||||
|
}
|
||||||
|
this.onmessage(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (event.type == "error") {
|
||||||
|
|
||||||
|
if (this.__timer) clearInterval(this.__timer);
|
||||||
|
if (this.onerror) this.onerror();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw "unknown event type: " + event.type;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e.toString());
|
console.error(e.toString());
|
||||||
|
@ -239,7 +219,7 @@
|
||||||
}
|
}
|
||||||
object.dispatchEvent(event, arguments);
|
object.dispatchEvent(event, arguments);
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-interface">DOM 2 EventInterface</a>}
|
* Basic implementation of {@link <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-interface">DOM 2 EventInterface</a>}
|
||||||
|
@ -300,6 +280,12 @@
|
||||||
|
|
||||||
WebSocket.__tasks = [];
|
WebSocket.__tasks = [];
|
||||||
|
|
||||||
|
WebSocket.loadFlashPolicyFile = function(url) {
|
||||||
|
WebSocket.__addTask(function() {
|
||||||
|
WebSocket.__flash.loadManualPolicyFile(url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
WebSocket.__initialize = function() {
|
WebSocket.__initialize = function() {
|
||||||
if (WebSocket.__swfLocation) {
|
if (WebSocket.__swfLocation) {
|
||||||
// For backword compatibility.
|
// For backword compatibility.
|
||||||
|
|
Loading…
Reference in New Issue