From 406208b9b97d5e01e91d109bb92eece5b075a16f Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Wed, 31 Aug 2011 16:13:02 -0500 Subject: close() should cancel create task. Change the init and close code so that the async __flash.create() task is cancelled in close() so that you can do something like this which tests whether the newer binary API exists: var wstest = new WebSocket('ws://localhost:57111'); if (typeof(wstest.binaryType) !== "undefined") { ... } wstest.close(); wstest = null; Without the change, a Flash error occurs later when the connect fails (because the close() call doesn't cancel the connect). --- web_socket.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/web_socket.js b/web_socket.js index 5397332..947a637 100644 --- a/web_socket.js +++ b/web_socket.js @@ -51,8 +51,9 @@ } // Uses setTimeout() to make sure __createFlash() runs after the caller sets ws.onopen etc. // Otherwise, when onopen fires immediately, onopen is called before it is set. - setTimeout(function() { + self.__createTask = setTimeout(function() { WebSocket.__addTask(function() { + self.__createTask = null; WebSocket.__flash.create( self.__id, url, protocols, proxyHost || null, proxyPort || 0, headers || null); }); @@ -89,6 +90,12 @@ * Close this web socket gracefully. */ WebSocket.prototype.close = function() { + if (this.__createTask) { + clearTimeout(this.__createTask); + this.__createTask = null; + this.readyState = WebSocket.CLOSED; + return; + } if (this.readyState == WebSocket.CLOSED || this.readyState == WebSocket.CLOSING) { return; } -- cgit v1.2.1