Skip to content

Add nsp prefix to socket.id for generating same ids with server #959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 + '#' + this.engine.id;
};

/**
* Mix in `Emitter`.
*/
Expand Down Expand Up @@ -358,7 +370,7 @@ Manager.prototype.socket = function (nsp) {
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
4 changes: 2 additions & 2 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down