diff --git a/lib/manager.js b/lib/manager.js index 8fb640305..3c92e36ad 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -481,13 +481,7 @@ Manager.prototype.disconnect = function () { debug('disconnect'); this.skipReconnect = true; this.reconnecting = false; - if ('opening' === this.readyState) { - // `onclose` will not fire because - // an open event never happened - this.cleanup(); - } - this.backoff.reset(); - this.readyState = 'closed'; + this.onclose('forced close'); if (this.engine) this.engine.close(); }; @@ -498,7 +492,7 @@ Manager.prototype.disconnect = function () { */ Manager.prototype.onclose = function (reason) { - debug('onclose'); + debug('closed due to %s', reason); this.cleanup(); this.backoff.reset(); diff --git a/test/socket.js b/test/socket.js index 91abd9d81..e3a82f8eb 100644 --- a/test/socket.js +++ b/test/socket.js @@ -176,4 +176,24 @@ describe('socket', function () { done(); }); }); + + it('should properly disconnect then reconnect', (done) => { + const socket = io('/', { forceNew: true, transports: ['websocket'] }); + + let count = 0; + + socket.once('connect', () => { + socket.disconnect().connect(); + }); + + socket.on('disconnect', () => { + count++; + }); + + setTimeout(() => { + expect(count).to.eql(1); + socket.disconnect(); + done(); + }, 200); + }); });