Skip to content

Commit

Permalink
Fixed problem with multiple request handling.
Browse files Browse the repository at this point in the history
Added proper cleanup
  • Loading branch information
rauchg committed Jun 13, 2011
1 parent c6185eb commit ea8cfc5
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,69 +68,93 @@ Transport.prototype.__defineGetter__('store', function () {

Transport.prototype.handleRequest = function (req) {
this.req = req;

if (req.method == 'GET') {
this.socket = req.socket;
this.open = true;
this.drained = true;
this.swapped = false;

this.log.debug('publishing that', this.id, 'connected');

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.log.info('request for existing session connection change');
self.close();
self.clearTimeouts();
self.clearHandlers();
});

if (!self.paused) {
self.subscribe();
}
});

if (!this.initHandlers) {
// handle forced disconnections
var self = this;
this.setHandlers();
this.onSocketConnect();
}
};

this.store.once('disconnect-force:' + this.id, function () {
self.onForcedDisconnect();
});
/**
* Called when a connection is first set.
*
* @api private
*/

this.store.on('heartbeat-clear:' + this.id, function () {
self.clearHeartbeatTimeout();
self.setHeartbeatInterval();
});
Transport.prototype.onSocketConnect = function () { };

this.store.on('volatile:' + this.id, function (packet) {
self.writeVolatile(packet);
});
/**
* Sets transport handlers
*
* @api private
*/

this.initHandlers = true;
}
Transport.prototype.setHandlers = function () {
var self = this;

this.store.once('disconnect-force:' + this.id, function () {
self.onForcedDisconnect();
});

if (!req.socket.__ioHandler) {
// add a handler only once per socket
this.socket.on('end', 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));
this.store.on('heartbeat-clear:' + this.id, function () {
self.clearHeartbeatTimeout();
self.setHeartbeatInterval();
});

this.onSocketConnect();
this.socket.__ioHandler = true;
}
}
this.store.on('volatile:' + this.id, function (packet) {
self.writeVolatile(packet);
});

this.bound = {
end: this.onSocketEnd.bind(this)
, close: this.onSocketClose.bind(this)
, error: this.onSocketError.bind(this)
, drain: this.onSocketDrain.bind(this)
};

this.socket.on('end', this.bound.end);
this.socket.on('close', this.bound.close);
this.socket.on('error', this.bound.error);
this.socket.on('drain', this.bound.drain);
};

/**
* Called when a connection is first set.
* Removes transport handlers
*
* @api private
*/

Transport.prototype.onSocketConnect = function () { };
Transport.prototype.clearHandlers = function () {
this.store.unsubscribe('disconnect-force:' + this.id);
this.store.unsubscribe('heartbeat-clear:' + this.id);
this.store.unsubscribe('volatile:' + this.id);

this.socket.removeListener('end', this.bound.end);
this.socket.removeListener('close', this.bound.close);
this.socket.removeListener('error', this.bound.error);
this.socket.removeListener('drain', this.bound.drain);
};

/**
* Called when the connection dies
Expand All @@ -142,9 +166,7 @@ Transport.prototype.onSocketEnd = function () {
// 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');
}
this.end(false, 'socket end');
};

/**
Expand All @@ -154,9 +176,7 @@ Transport.prototype.onSocketEnd = function () {
*/

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

/**
Expand Down

0 comments on commit ea8cfc5

Please sign in to comment.