Skip to content

Commit

Permalink
The socket.io Socket instance's emit was being manipulated which prev…
Browse files Browse the repository at this point in the history
…ented other events happening concurrently from being processed. The socket.io socket instance is now wrapped in a proxy object. The proxy object is manipulated now instead of the actual socket.io Socket instance
  • Loading branch information
NathanGRomano committed Sep 11, 2014
1 parent ea01177 commit 9e53bf9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
35 changes: 32 additions & 3 deletions lib/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var debug = require('debug')('router');
var debug = require('debug')('socket.io-events:router');
var util = require('util');
var emit = require('events').EventEmitter.prototype.emit;
var EventEmitter = require('events').EventEmitter;
var emit = EventEmitter.prototype.emit;
var slice = Array.prototype.slice;

module.exports = Router;
Expand Down Expand Up @@ -68,7 +69,7 @@ function Router () {
if (null != packet.id) {
args.push(this.ack(packet.id));
}
router.onRoute(null, this, args);
router.onRoute(null, Socket(this), args);
};

/*
Expand Down Expand Up @@ -337,3 +338,31 @@ Router.prototype._fns = function () {
}
return this.__fns;
};

/**
* Delegates to the socket. Used to capture messages and prevent the
* sock itself from being manipulated directly which is causing an issue
* with events not being processed.
*
* TODO support other API methods
*
* @api private
* @param {Socket} sock
*/

function Socket (sock) {
if (!(this instanceof Socket)) return new Socket(sock);
EventEmitter.call(this);
this.sock = sock;
this.domain = this.sock.domain;
this.id = this.sock.id;
this._events = this.sock._events;
this._maxListeners = this.sock._maxListeners;
}

util.inherits(Socket, EventEmitter);

Socket.prototype.emit = function () {
debug('debug wrapped');
return this.sock.emit.apply(this.sock, slice.call(arguments));
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "socket.io-events",
"version": "0.4.1",
"version": "0.4.2",
"description": "Power your socket.io apps with express like event routing.",
"main": "index.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion spec/lib/router-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ describe 'Router', ->
Given -> @packet = id:1, data: ['message', 'hello']
When -> @router.onEvent.call @socket, @packet
Then -> expect(@socket.ack).toHaveBeenCalledWith @packet.id
And -> expect(@router.onRoute).toHaveBeenCalledWith null, @socket, ['message', 'hello', @fn]
And -> expect(@router.onRoute).toHaveBeenCalledWith null, jasmine.any(Object), ['message', 'hello', @fn]
And -> expect(@router.onRoute.mostRecentCall.args[1].sock).toBe @socket

describe '#onRoute (err:Error=null, sock:Object, args:Array)', ->

Expand Down

0 comments on commit 9e53bf9

Please sign in to comment.