Skip to content

Commit

Permalink
Merge pull request #113 from Jxck/master
Browse files Browse the repository at this point in the history
#99 `packet` event
  • Loading branch information
rauchg committed Nov 5, 2012
2 parents 92df787 + 46f3104 commit 95f0614
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ Socket.prototype.onOpen = function () {

Socket.prototype.onPacket = function (packet) {
if ('open' == this.readyState) {
// export packet event
debug('packet');
this.emit('packet', packet);

// Reset ping timeout on any packet, incoming data is a good sign of
// other side's liveness
this.setPingTimeout();
Expand Down
31 changes: 31 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,37 @@ describe('server', function () {
});
});

describe('packet', function() {
it('should emit when socket receives packet', function (done) {
var engine = listen({ allowUpgrades: false }, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
engine.on('connection', function (conn) {
conn.on('packet', function (packet) {
expect(packet.type).to.be('message');
expect(packet.data).to.be('a');
done();
});
});
socket.on('open', function () {
socket.send('a');
});
});
});

it('should emit when receives ping', function (done) {
var engine = listen({ allowUpgrades: false, pingInterval: 4 }, function (port) {
var socket = new eioc.Socket('ws://localhost:%d'.s(port));
engine.on('connection', function (conn) {
conn.on('packet', function (packet) {
conn.close();
expect(packet.type).to.be('ping');
done();
});
});
});
});
});

describe('upgrade', function () {
it('should upgrade', function (done) {
var engine = listen(function (port) {
Expand Down

0 comments on commit 95f0614

Please sign in to comment.