From 558f8b26266a868b98691bc7258b33c445f8c7e0 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Mon, 24 Dec 2012 19:51:17 -0300 Subject: [PATCH] socket: improve keeping track of connected state --- lib/socket.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/socket.js b/lib/socket.js index 3f9bfc52f..7e13a6125 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -48,6 +48,7 @@ function Socket(io, nsp){ this.open(); this.buffer = []; this.connected = false; + this.disconnected = true; } /** @@ -170,6 +171,8 @@ Socket.prototype.onopen = function(){ Socket.prototype.onclose = function(reason){ debug('close (%s)', reason); + this.connected = false; + this.disconnected = true; this.emit('disconnect', reason); }; @@ -272,8 +275,9 @@ Socket.prototype.onack = function(packet){ */ Socket.prototype.onconnect = function(){ - this.emit('connect'); this.connected = true; + this.disconnected = false; + this.emit('connect'); this.emitBuffered(); }; @@ -299,6 +303,7 @@ Socket.prototype.emitBuffered = function(){ Socket.prototype.ondisconnect = function(){ debug('server disconnect (%s)', this.nsp); this.destroy(); + this.onclose('io server disconnect'); }; /** @@ -328,17 +333,13 @@ Socket.prototype.destroy = function(){ Socket.prototype.close = Socket.prototype.disconnect = function(){ - debug('performing disconnect (%s)', this.nsp); + if (!this.connected) return this; - this.packet(parser.PACKET_DISCONNECT); - - // manual close means no reconnect - for (var i = 0; i < this.subs.length; i++) { - this.subs[i].destroy(); - } + debug('performing disconnect (%s)', this.nsp); + this.packet({ type: parser.PACKET_DISCONNECT }); - // notify manager - this.io.destroy(this); + // destroy subscriptions + this.destroy(); // fire events this.onclose('io client disconnect');