Skip to content

Commit

Permalink
Changed behavior of end and close events, and introduced `swapped…
Browse files Browse the repository at this point in the history
…` semaphore.
  • Loading branch information
rauchg committed Jun 12, 2011
1 parent 9cc1e77 commit 1743bab
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ Transport.prototype.handleRequest = function (req) {
this.socket = req.socket;
this.open = true;
this.drained = true;
this.swapped = false;

var self = this;

this.log.debug('publishing that', this.id, 'connected');
this.store.publish('open:' + this.id, function () {
self.store.once('open:' + self.id, function () {
self.log.debug('request for existing session connection change');
self.swapped = true;
self.close();
self.clearTimeouts();
});
Expand Down Expand Up @@ -112,7 +114,7 @@ Transport.prototype.handleRequest = function (req) {
if (!req.socket.__ioHandler) {
// add a handler only once per socket
this.socket.on('end', this.onSocketEnd.bind(this));
this.socket.on('close', this.onSocketEnd.bind(this));
this.socket.on('close', this.onSocketClose.bind(this));
this.socket.on('error', this.onSocketError.bind(this));
this.socket.on('drain', this.onSocketDrain.bind(this));

Expand All @@ -137,7 +139,24 @@ Transport.prototype.onSocketConnect = function () { };
*/

Transport.prototype.onSocketEnd = function () {
this.close();
// we check that the socket wasn't swapped
// we don't want to sever a connection that's not active, since we don't kill
// inactive sockets that the browser might reuse for other purposes
if (!this.swapped) {
this.end(false, 'socket end');
}
};

/**
* Called when the connection dies
*
* @api private
*/

Transport.prototype.onSocketClose = function (error) {
if (!this.swapped) {
this.end(false, error ? 'socket error' : 'socket close');
}
};

/**
Expand Down

0 comments on commit 1743bab

Please sign in to comment.