From 46f310462b209994f1e55336a55f7830935fafad Mon Sep 17 00:00:00 2001 From: Jxck Date: Mon, 5 Nov 2012 21:18:25 +0900 Subject: [PATCH] add packet event to socket --- lib/socket.js | 4 ++++ test/server.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/socket.js b/lib/socket.js index b1567e8a9..8cb799b92 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -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(); diff --git a/test/server.js b/test/server.js index 437541f52..5dc2f3f4d 100644 --- a/test/server.js +++ b/test/server.js @@ -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) {