Skip to content
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

Add nsp prefix to socket.id #1058

Merged
merged 2 commits into from
Jan 13, 2017
Merged
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 === '/' ? '' : (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