diff --git a/lib/manager.ts b/lib/manager.ts index 4352a956e..b034cc727 100644 --- a/lib/manager.ts +++ b/lib/manager.ts @@ -292,7 +292,6 @@ export class Manager extends Emitter { private _reconnectionDelayMax: number; private _timeout: any; - private connecting: Array = []; private encoder: Encoder; private decoder: Decoder; private skipReconnect: boolean; @@ -601,19 +600,6 @@ export class Manager extends Emitter { if (!socket) { socket = new Socket(this, nsp, opts); this.nsps[nsp] = socket; - var self = this; - socket.on("connecting", onConnecting); - - if (this._autoConnect) { - // manually call here since connecting event is fired before listening - onConnecting(); - } - } - - function onConnecting() { - if (!~self.connecting.indexOf(socket)) { - self.connecting.push(socket); - } } return socket; @@ -626,9 +612,16 @@ export class Manager extends Emitter { * @private */ _destroy(socket: Socket) { - const index = this.connecting.indexOf(socket); - if (~index) this.connecting.splice(index, 1); - if (this.connecting.length) return; + const nsps = Object.keys(this.nsps); + + for (const nsp of nsps) { + const socket = this.nsps[nsp]; + + if (socket.active) { + debug("socket %s is still active, skipping close", nsp); + return; + } + } this._close(); } diff --git a/lib/socket.ts b/lib/socket.ts index b972d0927..743686648 100644 --- a/lib/socket.ts +++ b/lib/socket.ts @@ -88,6 +88,13 @@ export class Socket extends Emitter { ]; } + /** + * Whether the Socket will try to reconnect when its Manager connects or reconnects + */ + public get active(): boolean { + return !!this.subs; + } + /** * "Opens" the socket. * @@ -343,6 +350,7 @@ export class Socket extends Emitter { * @private */ private onconnect(id: string) { + debug("socket connected with id %s", id); this.id = id; this.connected = true; this.disconnected = false; diff --git a/test/connection.ts b/test/connection.ts index 816928aa5..e35eceda4 100644 --- a/test/connection.ts +++ b/test/connection.ts @@ -427,6 +427,33 @@ describe("connection", function () { }); }); + it("should not close the connection when disconnecting a single socket", (done) => { + const manager = new Manager({ + autoConnect: false, + }); + const socket1 = manager.socket("/foo"); + const socket2 = manager.socket("/asd"); + + socket1.connect(); + socket1.on("connect", () => { + socket2.connect(); + }); + + socket2.on("connect", () => { + socket2.on("disconnect", () => { + done(new Error("should not happen for now")); + }); + socket1.disconnect(); + setTimeout(() => { + socket2.off("disconnect"); + manager.on("close", () => { + done(); + }); + socket2.disconnect(); + }, 200); + }); + }); + // Ignore incorrect connection test for old IE due to no support for // `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail) if (!global.document || hasCORS) {