From 9d518f1fd80e806f4293f02d87f2297f55928cc2 Mon Sep 17 00:00:00 2001 From: nkzawa Date: Tue, 12 Apr 2016 07:27:36 +0900 Subject: [PATCH 1/2] add nsp prefix to socket.id --- lib/manager.js | 16 ++++++++++++++-- test/socket.js | 4 ++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/manager.js b/lib/manager.js index 1ad3ead32..d2fecd1b5 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 + '#' + 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 1fa0b6c59..dee935e16 100644 --- a/test/socket.js +++ b/test/socket.js @@ -4,11 +4,11 @@ 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 nsp + the engine.io socket id', function (done) { var socket = io({ forceNew: true }); socket.on('connect', function () { expect(socket.id).to.be.ok(); - expect(socket.id).to.eql(socket.io.engine.id); + expect(socket.id).to.eql('/#' + socket.io.engine.id); socket.disconnect(); done(); }); From c1cdccb64169b8e665fbe0c80d0e0c507ad7448e Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 13 Jan 2017 11:18:05 +0100 Subject: [PATCH 2/2] Actually commit something --- lib/manager.js | 2 +- test/socket.js | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/manager.js b/lib/manager.js index d2fecd1b5..b08482a84 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -106,7 +106,7 @@ Manager.prototype.updateSocketIds = function () { */ Manager.prototype.generateId = function (nsp) { - return nsp + '#' + this.engine.id; + return (nsp === '/' ? '' : (nsp + '#')) + this.engine.id; }; /** diff --git a/test/socket.js b/test/socket.js index dee935e16..3a01d2fc9 100644 --- a/test/socket.js +++ b/test/socket.js @@ -4,11 +4,21 @@ var io = require('../'); describe('socket', function () { this.timeout(70000); - it('should have an accessible socket id equal to nsp + 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(); - expect(socket.id).to.eql('/#' + socket.io.engine.id); + expect(socket.id).to.eql(socket.io.engine.id); + socket.disconnect(); + done(); + }); + }); + + 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(); });