Skip to content

Commit

Permalink
namespace: make sure not to fire connection if underlying client cl…
Browse files Browse the repository at this point in the history
…osed after `next` is called from a middleware
  • Loading branch information
rauchg committed Dec 24, 2012
1 parent 01a06b2 commit 5f843fe
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,25 @@ Namespace.prototype.add = function(client, fn){
var self = this;
this.run(socket, function(err){
process.nextTick(function(){
if (err) return socket.err(err.data || err.message);

// track socket
self.sockets.push(socket);

// it's paramount that the internal `onconnect` logic
// fires before user-set events to prevent state order
// violations (such as a disconnection before the connection
// logic is complete)
socket.onconnect();
if (fn) fn();

// fire user-set events
self.emit('connect', socket);
self.emit('connection', socket);
if ('open' == client.conn.readyState) {
if (err) return socket.error(err.data || err.message);

// track socket
self.sockets.push(socket);

// it's paramount that the internal `onconnect` logic
// fires before user-set events to prevent state order
// violations (such as a disconnection before the connection
// logic is complete)
socket.onconnect();
if (fn) fn();

// fire user-set events
self.emit('connect', socket);
self.emit('connection', socket);
} else {
debug('next called after client was closed - ignoring socket');
}
});
});
return socket;
Expand Down

0 comments on commit 5f843fe

Please sign in to comment.