Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ function Socket (id, server, transport, req) {
this.packetsFn = [];
this.sentCallbackFn = [];
this.request = req;

this.checkIntervalTimer = null;
this.upgradeTimeoutTimer = null;
this.pingTimeoutTimer = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to initialize them as null ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because that will a) make it clear to the programmer what kinds of attributes this class will have and b) improve performance because the VM will not have to change the internal class at runtime.


this.setTransport(transport);
this.onOpen();
Expand Down Expand Up @@ -158,6 +162,7 @@ Socket.prototype.maybeUpgrade = function (transport) {
self.upgradeTimeoutTimer = setTimeout(function () {
debug('client did not complete upgrade - closing transport');
clearInterval(self.checkIntervalTimer);
self.checkIntervalTimer = null;
if ('open' == transport.readyState) {
transport.close();
}
Expand All @@ -166,6 +171,7 @@ Socket.prototype.maybeUpgrade = function (transport) {
function onPacket(packet){
if ('ping' == packet.type && 'probe' == packet.data) {
transport.send([{ type: 'pong', data: 'probe' }]);
clearInterval(self.checkIntervalTimer);
self.checkIntervalTimer = setInterval(check, 100);
} else if ('upgrade' == packet.type && self.readyState == 'open') {
debug('got upgrade packet - upgrading');
Expand All @@ -176,6 +182,7 @@ Socket.prototype.maybeUpgrade = function (transport) {
self.setPingTimeout();
self.flush();
clearInterval(self.checkIntervalTimer);
self.checkIntervalTimer = null;
clearTimeout(self.upgradeTimeoutTimer);
transport.removeListener('packet', onPacket);
} else {
Expand Down Expand Up @@ -219,11 +226,12 @@ Socket.prototype.onClose = function (reason, description) {
if ('closed' != this.readyState) {
clearTimeout(this.pingTimeoutTimer);
clearInterval(this.checkIntervalTimer);
this.checkIntervalTimer = null;
clearTimeout(this.upgradeTimeoutTimer);
var self = this;
// clean writeBuffer in next tick, so developers can still
// grab the writeBuffer on 'close' event
process.nextTick(function() {
setImmediate(function() {
self.writeBuffer = [];
});
this.packetsFn = [];
Expand Down