diff --git a/lib/router.js b/lib/router.js index 09f4155..d3748da 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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; @@ -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); }; /* @@ -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)); +}; diff --git a/package.json b/package.json index 65c072b..f63e5d8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/spec/lib/router-spec.coffee b/spec/lib/router-spec.coffee index 9d80e24..de48c80 100644 --- a/spec/lib/router-spec.coffee +++ b/spec/lib/router-spec.coffee @@ -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)', ->