Skip to content
Browse files

Fixing a bug that WebSocket#send throws TypeError instead of INVALID_…

…STATE_ERR when Flash object is not initialized.
  • Loading branch information...
1 parent b674728 commit de0e9eb41ab7e8b306d3663fb78551b5867e70e6 @gimite gimite committed Jul 3, 2010
Showing with 15 additions and 16 deletions.
  1. +15 −16 web_socket.js
View
31 web_socket.js
@@ -46,28 +46,25 @@
WebSocket.__flash.create(url, protocol, proxyHost || null, proxyPort || 0, headers || null);
self.__flash.addEventListener("open", function(fe) {
- self.readyState = self.__flash.getReadyState();
-
- 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);
- }
-
try {
+ self.readyState = self.__flash.getReadyState();
+ 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) {
- self.readyState = self.__flash.getReadyState();
-
- if (self.__timer) clearInterval(self.__timer);
try {
+ self.readyState = self.__flash.getReadyState();
+ if (self.__timer) clearInterval(self.__timer);
if (self.onclose) self.onclose();
} catch (e) {
console.error(e.toString());
@@ -83,8 +80,8 @@
});
self.__flash.addEventListener("error", function(fe) {
- if (self.__timer) clearInterval(self.__timer);
try {
+ if (self.__timer) clearInterval(self.__timer);
if (self.onerror) self.onerror();
} catch (e) {
console.error(e.toString());
@@ -93,7 +90,7 @@
self.__flash.addEventListener("stateChange", function(fe) {
try {
- self.readyState = fe.getReadyState();
+ self.readyState = self.__flash.getReadyState();
self.bufferedAmount = fe.getBufferedAmount();
} catch (e) {
console.error(e.toString());
@@ -104,7 +101,9 @@
};
WebSocket.prototype.send = function(data) {
- this.readyState = this.__flash.getReadyState();
+ if (this.__flash) {
+ this.readyState = this.__flash.getReadyState();
+ }
if (!this.__flash || this.readyState == WebSocket.CONNECTING) {
throw "INVALID_STATE_ERR: Web Socket connection has not been established";
}

0 comments on commit de0e9eb

Please sign in to comment.
Something went wrong with that request. Please try again.