Browse files

add packet event to socket

  • Loading branch information...
1 parent 92df787 commit 46f310462b209994f1e55336a55f7830935fafad @Jxck Jxck committed Nov 5, 2012
Showing with 35 additions and 0 deletions.
  1. +4 −0 lib/socket.js
  2. +31 −0 test/server.js
View
4 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();
View
31 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) {

0 comments on commit 46f3104

Please sign in to comment.