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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function Socket(io, nsp){
this.open();
this.buffer = [];
this.connected = false;
this.disconnected = true;
}

/**
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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();
};

Expand All @@ -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');
};

/**
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 558f8b2

Please sign in to comment.