Skip to content

Commit

Permalink
[fix] Add nsp prefix to socket.id (#1058)
Browse files Browse the repository at this point in the history
So the socket ids on the client and on the server are equals.
  • Loading branch information
darrachequesne authored Jan 13, 2017
1 parent ba5dca3 commit fcb5c43
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 () {
Expand Down

0 comments on commit fcb5c43

Please sign in to comment.