Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down