Skip to content

Commit fcb5c43

Browse files
[fix] Add nsp prefix to socket.id (#1058)
So the socket ids on the client and on the server are equals.
1 parent ba5dca3 commit fcb5c43

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/manager.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,23 @@ Manager.prototype.emitAll = function () {
9292
Manager.prototype.updateSocketIds = function () {
9393
for (var nsp in this.nsps) {
9494
if (has.call(this.nsps, nsp)) {
95-
this.nsps[nsp].id = this.engine.id;
95+
this.nsps[nsp].id = this.generateId(nsp);
9696
}
9797
}
9898
};
9999

100+
/**
101+
* generate `socket.id` for the given `nsp`
102+
*
103+
* @param {String} nsp
104+
* @return {String}
105+
* @api private
106+
*/
107+
108+
Manager.prototype.generateId = function (nsp) {
109+
return (nsp === '/' ? '' : (nsp + '#')) + this.engine.id;
110+
};
111+
100112
/**
101113
* Mix in `Emitter`.
102114
*/
@@ -358,7 +370,7 @@ Manager.prototype.socket = function (nsp, opts) {
358370
var self = this;
359371
socket.on('connecting', onConnecting);
360372
socket.on('connect', function () {
361-
socket.id = self.engine.id;
373+
socket.id = self.generateId(nsp);
362374
});
363375

364376
if (this.autoConnect) {

test/socket.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var io = require('../');
44
describe('socket', function () {
55
this.timeout(70000);
66

7-
it('should have an accessible socket id equal to the engine.io socket id', function (done) {
7+
it('should have an accessible socket id equal to the server-side socket id (default namespace)', function (done) {
88
var socket = io({ forceNew: true });
99
socket.on('connect', function () {
1010
expect(socket.id).to.be.ok();
@@ -14,6 +14,16 @@ describe('socket', function () {
1414
});
1515
});
1616

17+
it('should have an accessible socket id equal to the server-side socket id (custom namespace)', function (done) {
18+
var socket = io('/foo', { forceNew: true });
19+
socket.on('connect', function () {
20+
expect(socket.id).to.be.ok();
21+
expect(socket.id).to.eql('/foo#' + socket.io.engine.id);
22+
socket.disconnect();
23+
done();
24+
});
25+
});
26+
1727
it('clears socket.id upon disconnection', function (done) {
1828
var socket = io({ forceNew: true });
1929
socket.on('connect', function () {

0 commit comments

Comments
 (0)