Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket: Accept a PONG #1026

Merged
merged 1 commit into from
Sep 9, 2012
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
108 changes: 62 additions & 46 deletions lib/transports/websocket/hybi-16.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,62 @@ function Parser () {
this.currentMessage = '';

var self = this;

function onPingOrPong (data, pingpong) {
if (self.state.lastFragment == false) {
self.error('fragmented ping is not supported');
return;
}

var finish = function(mask, finaldata) {
self.emit(pingpong, self.unmask(mask, finaldata));
self.endPacket();
};

var expectData = function(length, mask) {
if (length == 0) {
finish(null,null);
}
else {
self.expect('Data', length, function(finaldata) {
finish(mask, finaldata);
});
}
};

var checkMask = function (length) {
if (self.state.masked) {
self.expect('Mask', 4, function(mask) {
expectData(length, mask);
});
}
else {
expectData(length, null);
}
};

var getLength = function () {
// decode length
var firstLength = data[1] & 0x7f;

if (firstLength < 126) {
checkMask(firstLength);
}
else if (firstLength == 126) {
self.expect('Length', 2, function(data) {
checkMask(util.unpack(data));
});
}
else if (firstLength == 127) {
self.expect('Length', 8, function(data) {
checkMask(util.unpack(data));
});
}
};

getLength(); //step1
}

this.opcodeHandlers = {
// text
'1': function(data) {
Expand Down Expand Up @@ -384,55 +440,15 @@ function Parser () {
},
// ping
'9': function(data) {
if (self.state.lastFragment == false) {
self.error('fragmented ping is not supported');
return;
}

var finish = function(mask, data) {
self.emit('ping', self.unmask(mask, data));
self.endPacket();
}

var expectData = function(length) {
if (self.state.masked) {
self.expect('Mask', 4, function(data) {
var mask = data;
self.expect('Data', length, function(data) {
finish(mask, data);
});
});
}
else {
self.expect('Data', length, function(data) {
finish(null, data);
});
}
}

// decode length
var firstLength = data[1] & 0x7f;
if (firstLength == 0) {
finish(null, null);
}
else if (firstLength < 126) {
expectData(firstLength);
}
else if (firstLength == 126) {
self.expect('Length', 2, function(data) {
expectData(util.unpack(data));
});
}
else if (firstLength == 127) {
self.expect('Length', 8, function(data) {
expectData(util.unpack(data));
});
}
onPingOrPong(data,'ping');
},
'10': function(data) {
onPingOrPong(data,'pong');
}
}
};

this.expect('Opcode', 2, this.processPacket);
};
}

/**
* Inherits from EventEmitter.
Expand Down