Skip to content

Commit

Permalink
socket: improve keeping track of connected state
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Dec 24, 2012
1 parent a2f1729 commit 558f8b2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/socket.js
Expand Up @@ -48,6 +48,7 @@ function Socket(io, nsp){
this.open(); this.open();
this.buffer = []; this.buffer = [];
this.connected = false; this.connected = false;
this.disconnected = true;
} }


/** /**
Expand Down Expand Up @@ -170,6 +171,8 @@ Socket.prototype.onopen = function(){


Socket.prototype.onclose = function(reason){ Socket.prototype.onclose = function(reason){
debug('close (%s)', reason); debug('close (%s)', reason);
this.connected = false;
this.disconnected = true;
this.emit('disconnect', reason); this.emit('disconnect', reason);
}; };


Expand Down Expand Up @@ -272,8 +275,9 @@ Socket.prototype.onack = function(packet){
*/ */


Socket.prototype.onconnect = function(){ Socket.prototype.onconnect = function(){
this.emit('connect');
this.connected = true; this.connected = true;
this.disconnected = false;
this.emit('connect');
this.emitBuffered(); this.emitBuffered();
}; };


Expand All @@ -299,6 +303,7 @@ Socket.prototype.emitBuffered = function(){
Socket.prototype.ondisconnect = function(){ Socket.prototype.ondisconnect = function(){
debug('server disconnect (%s)', this.nsp); debug('server disconnect (%s)', this.nsp);
this.destroy(); this.destroy();
this.onclose('io server disconnect');
}; };


/** /**
Expand Down Expand Up @@ -328,17 +333,13 @@ Socket.prototype.destroy = function(){


Socket.prototype.close = Socket.prototype.close =
Socket.prototype.disconnect = function(){ Socket.prototype.disconnect = function(){
debug('performing disconnect (%s)', this.nsp); if (!this.connected) return this;


this.packet(parser.PACKET_DISCONNECT); debug('performing disconnect (%s)', this.nsp);

this.packet({ type: parser.PACKET_DISCONNECT });
// manual close means no reconnect
for (var i = 0; i < this.subs.length; i++) {
this.subs[i].destroy();
}


// notify manager // destroy subscriptions
this.io.destroy(this); this.destroy();


// fire events // fire events
this.onclose('io client disconnect'); this.onclose('io client disconnect');
Expand Down

0 comments on commit 558f8b2

Please sign in to comment.