diff --git a/lib/manager.js b/lib/manager.js index 1ad3ead325..b08482a84d 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -92,11 +92,23 @@ Manager.prototype.emitAll = function () { Manager.prototype.updateSocketIds = function () { for (var nsp in this.nsps) { if (has.call(this.nsps, nsp)) { - this.nsps[nsp].id = this.engine.id; + this.nsps[nsp].id = this.generateId(nsp); } } }; +/** + * generate `socket.id` for the given `nsp` + * + * @param {String} nsp + * @return {String} + * @api private + */ + +Manager.prototype.generateId = function (nsp) { + return (nsp === '/' ? '' : (nsp + '#')) + this.engine.id; +}; + /** * Mix in `Emitter`. */ @@ -358,7 +370,7 @@ Manager.prototype.socket = function (nsp, opts) { var self = this; socket.on('connecting', onConnecting); socket.on('connect', function () { - socket.id = self.engine.id; + socket.id = self.generateId(nsp); }); if (this.autoConnect) { diff --git a/test/socket.js b/test/socket.js index 1fa0b6c59b..3a01d2fc96 100644 --- a/test/socket.js +++ b/test/socket.js @@ -4,7 +4,7 @@ var io = require('../'); describe('socket', function () { this.timeout(70000); - it('should have an accessible socket id equal to the engine.io socket id', function (done) { + it('should have an accessible socket id equal to the server-side socket id (default namespace)', function (done) { var socket = io({ forceNew: true }); socket.on('connect', function () { expect(socket.id).to.be.ok(); @@ -14,6 +14,16 @@ describe('socket', function () { }); }); + it('should have an accessible socket id equal to the server-side socket id (custom namespace)', function (done) { + var socket = io('/foo', { forceNew: true }); + socket.on('connect', function () { + expect(socket.id).to.be.ok(); + expect(socket.id).to.eql('/foo#' + socket.io.engine.id); + socket.disconnect(); + done(); + }); + }); + it('clears socket.id upon disconnection', function (done) { var socket = io({ forceNew: true }); socket.on('connect', function () {