diff --git a/lib/socket.js b/lib/socket.js index df89de624..f6b767d11 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -300,7 +300,7 @@ */ Socket.prototype.disconnect = function () { - if (this.connected) { + if (this.connected || this.connecting) { if (this.open) { this.of('').packet({ type: 'disconnect' }); } @@ -405,9 +405,11 @@ Socket.prototype.onError = function (err) { if (err && err.advice) { - if (this.options.reconnect && err.advice === 'reconnect' && this.connected) { + if (err.advice === 'reconnect' && (this.connected || this.connecting)) { this.disconnect(); - this.reconnect(); + if (this.options.reconnect) { + this.reconnect(); + } } } @@ -421,19 +423,22 @@ */ Socket.prototype.onDisconnect = function (reason) { - var wasConnected = this.connected; + var wasConnected = this.connected + , wasConnecting = this.connecting; this.connected = false; this.connecting = false; this.open = false; - if (wasConnected) { + if (wasConnected || wasConnecting) { this.transport.close(); this.transport.clearTimeouts(); - this.publish('disconnect', reason); + if (wasConnected) { + this.publish('disconnect', reason); - if ('booted' != reason && this.options.reconnect && !this.reconnecting) { - this.reconnect(); + if ('booted' != reason && this.options.reconnect && !this.reconnecting) { + this.reconnect(); + } } } };