Skip to content

Commit

Permalink
feat: do not reuse the Engine.IO id
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Oct 8, 2020
1 parent 249e0be commit bbe94ad
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 79 deletions.
14 changes: 0 additions & 14 deletions build/manager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ export declare class Manager extends Emitter {
* @api private
*/
emitAll(event: string, arg?: any): void;
/**
* Update `socket.id` of all sockets
*
* @api private
*/
updateSocketIds(): void;
/**
* generate `socket.id` for the given `nsp`
*
* @param {String} nsp
* @return {String}
* @api private
*/
generateId(nsp: any): string;
/**
* Sets the `reconnection` config.
*
Expand Down
26 changes: 0 additions & 26 deletions build/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,6 @@ class Manager extends component_emitter_1.default {
}
}
}
/**
* Update `socket.id` of all sockets
*
* @api private
*/
updateSocketIds() {
for (let nsp in this.nsps) {
if (has.call(this.nsps, nsp)) {
this.nsps[nsp].id = this.generateId(nsp);
}
}
}
/**
* generate `socket.id` for the given `nsp`
*
* @param {String} nsp
* @return {String}
* @api private
*/
generateId(nsp) {
return (nsp === "/" ? "" : nsp + "#") + this.engine.id;
}
/**
* Sets the `reconnection` config.
*
Expand Down Expand Up @@ -386,9 +364,6 @@ class Manager extends component_emitter_1.default {
this.nsps[nsp] = socket;
var self = this;
socket.on("connecting", onConnecting);
socket.on("connect", function () {
socket.id = self.generateId(nsp);
});
if (this.autoConnect) {
// manually call here since connecting event is fired before listening
onConnecting();
Expand Down Expand Up @@ -572,7 +547,6 @@ class Manager extends component_emitter_1.default {
const attempt = this.backoff.attempts;
this.reconnecting = false;
this.backoff.reset();
this.updateSocketIds();
this.emitAll("reconnect", attempt);
}
}
Expand Down
2 changes: 1 addition & 1 deletion build/socket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export declare class Socket extends Emitter {
*
* @api private
*/
onconnect(): void;
onconnect(id: string): void;
/**
* Emit buffered events (received and emitted).
*
Expand Down
6 changes: 4 additions & 2 deletions build/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ class Socket extends component_emitter_1.default {
return;
switch (packet.type) {
case socket_io_parser_1.PacketType.CONNECT:
this.onconnect();
const id = packet.data.sid;
this.onconnect(id);
break;
case socket_io_parser_1.PacketType.EVENT:
this.onevent(packet);
Expand Down Expand Up @@ -289,7 +290,8 @@ class Socket extends component_emitter_1.default {
*
* @api private
*/
onconnect() {
onconnect(id) {
this.id = id;
this.connected = true;
this.disconnected = false;
this.emit("connect");
Expand Down
28 changes: 0 additions & 28 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,30 +93,6 @@ export class Manager extends Emitter {
}
}

/**
* Update `socket.id` of all sockets
*
* @api private
*/
updateSocketIds() {
for (let nsp in this.nsps) {
if (has.call(this.nsps, nsp)) {
this.nsps[nsp].id = this.generateId(nsp);
}
}
}

/**
* generate `socket.id` for the given `nsp`
*
* @param {String} nsp
* @return {String}
* @api private
*/
generateId(nsp) {
return (nsp === "/" ? "" : nsp + "#") + this.engine.id;
}

/**
* Sets the `reconnection` config.
*
Expand Down Expand Up @@ -417,9 +393,6 @@ export class Manager extends Emitter {
this.nsps[nsp] = socket;
var self = this;
socket.on("connecting", onConnecting);
socket.on("connect", function () {
socket.id = self.generateId(nsp);
});

if (this.autoConnect) {
// manually call here since connecting event is fired before listening
Expand Down Expand Up @@ -618,7 +591,6 @@ export class Manager extends Emitter {
const attempt = this.backoff.attempts;
this.reconnecting = false;
this.backoff.reset();
this.updateSocketIds();
this.emitAll("reconnect", attempt);
}
}
6 changes: 4 additions & 2 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ export class Socket extends Emitter {

switch (packet.type) {
case PacketType.CONNECT:
this.onconnect();
const id = packet.data.sid;
this.onconnect(id);
break;

case PacketType.EVENT:
Expand Down Expand Up @@ -319,7 +320,8 @@ export class Socket extends Emitter {
*
* @api private
*/
onconnect() {
onconnect(id: string) {
this.id = id;
this.connected = true;
this.disconnected = false;
this.emit("connect");
Expand Down
11 changes: 7 additions & 4 deletions test/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ describe("socket", function () {

it("should have an accessible socket id equal to the server-side socket id (default namespace)", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {

socket.emit("getId", (id) => {
expect(socket.id).to.be.ok();
expect(socket.id).to.eql(socket.io.engine.id);
expect(socket.id).to.be.eql(id);
expect(socket.id).to.not.eql(socket.io.engine.id);
socket.disconnect();
done();
});
});

it("should have an accessible socket id equal to the server-side socket id (custom namespace)", (done) => {
const socket = io("/foo", { forceNew: true });
socket.on("connect", () => {
socket.emit("getId", (id) => {
expect(socket.id).to.be.ok();
expect(socket.id).to.eql("/foo#" + socket.io.engine.id);
expect(socket.id).to.be.eql(id);
expect(socket.id).to.not.eql(socket.io.engine.id);
socket.disconnect();
done();
});
Expand Down
10 changes: 8 additions & 2 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const io = require("socket.io");
const server = io(process.env.ZUUL_PORT || 3210, { pingInterval: 2000 });
const expect = require("expect.js");

server.of("/foo").on("connection", () => {
// register namespace
server.of("/foo").on("connection", (socket) => {
socket.on("getId", (cb) => {
cb(socket.id);
});
});

server.of("/timeout_socket").on("connection", () => {
Expand Down Expand Up @@ -145,4 +147,8 @@ server.on("connection", (socket) => {
socket.on("getHandshake", (cb) => {
cb(socket.handshake);
});

socket.on("getId", (cb) => {
cb(socket.id);
});
});

0 comments on commit bbe94ad

Please sign in to comment.