Skip to content

Commit

Permalink
Making onclose event fired by ws.close() asynchronous.
Browse files Browse the repository at this point in the history
  • Loading branch information
gimite committed Sep 13, 2010
1 parent 9e76637 commit 53b0887
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions web_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,21 @@
};

WebSocket.prototype.close = function() {
if (!this.__flash) return;
this.readyState = this.__flash.getReadyState();
if (this.readyState != WebSocket.OPEN) return;
this.__flash.close();
var self = this;
if (!self.__flash) return;
self.readyState = self.__flash.getReadyState();
if (self.readyState != WebSocket.OPEN) return;
self.__flash.close();
// Sets/calls them manually here because Flash WebSocketConnection.close cannot fire events
// which causes weird error:
// > You are trying to call recursively into the Flash Player which is not allowed.
this.readyState = WebSocket.CLOSED;
if (this.__timer) clearInterval(this.__timer);
if (this.onclose) this.onclose();
self.readyState = WebSocket.CLOSED;
if (self.__timer) clearInterval(self.__timer);
if (self.onclose) {
// Make it asynchronous so that it looks more like an actual
// close event
setTimeout(self.onclose, 1);
}
};

/**
Expand Down

0 comments on commit 53b0887

Please sign in to comment.