From 3280f38c27f4f96d117ff6fa01a69b7acb526ab7 Mon Sep 17 00:00:00 2001 From: Greg Soltis Date: Mon, 15 Oct 2012 11:34:44 -0700 Subject: [PATCH] Fix check of readyState for immediate close --- lib/faye/websocket/api.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/faye/websocket/api.js b/lib/faye/websocket/api.js index 5ba7b44..5f1ab04 100644 --- a/lib/faye/websocket/api.js +++ b/lib/faye/websocket/api.js @@ -57,9 +57,7 @@ var API = { close: function(code, reason, ack) { if (this.readyState === API.CLOSING || this.readyState === API.CLOSED) return; - - this.readyState = API.CLOSING; - + var close = function() { this.readyState = API.CLOSED; if (this._pingLoop) clearInterval(this._pingLoop); @@ -68,15 +66,19 @@ var API = { event.initEvent('close', false, false); this.dispatchEvent(event); }; - + if (this.readyState === API.CONNECTING) { close.call(this); - } else if (ack !== false) { - if (this._parser.close) this._parser.close(code, reason, close, this); - else close.call(this); } else { - if (this._parser.close) this._parser.close(code, reason); - close.call(this); + this.readyState = API.CLOSING; + + if (ack !== false) { + if (this._parser.close) this._parser.close(code, reason, close, this); + else close.call(this); + } else { + if (this._parser.close) this._parser.close(code, reason); + close.call(this); + } } } };