Permalink
Browse files

add packetCreate event for #99

  • Loading branch information...
1 parent ec9334d commit b4d60687acd93d4aa19f83290e6ea6e1427f135d @Jxck Jxck committed Nov 6, 2012
Showing with 38 additions and 1 deletion.
  1. +9 −1 lib/socket.js
  2. +29 −0 test/server.js
View
@@ -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);
View
@@ -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) {

0 comments on commit b4d6068

Please sign in to comment.