diff --git a/lib/socket.js b/lib/socket.js index 8cb799b92..fc3d202ea 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -276,7 +276,15 @@ Socket.prototype.send = function (data, callback) { Socket.prototype.sendPacket = function (type, data, callback) { if ('closing' != this.readyState) { debug('sending packet "%s" (%s)', type, data); - this.writeBuffer.push({ type: type, data: data }); + + var packet = { type: type }; + if (data) packet.data = data; + + // exports packetCreate event + this.emit('packetCreate', packet); + + this.writeBuffer.push(packet); + //add send callback to object if (callback) { this.packetsFn.push(callback); diff --git a/test/server.js b/test/server.js index 1f078c1ce..94254d8ae 100644 --- a/test/server.js +++ b/test/server.js @@ -942,6 +942,35 @@ describe('server', function () { }); }); + describe('packetCreate', function() { + it('should emit before socket send message', 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('packetCreate', function(packet) { + expect(packet.type).to.be('message'); + expect(packet.data).to.be('a'); + done(); + }); + conn.send('a'); + }); + }); + }); + + it('should emit before send pong', 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('packetCreate', function (packet) { + conn.close(); + expect(packet.type).to.be('pong'); + done(); + }); + }); + }); + }); + }); + describe('upgrade', function () { it('should upgrade', function (done) { var engine = listen(function (port) {