Skip to content

Commit

Permalink
fix: check if client connected when reconnecting (#1162)
Browse files Browse the repository at this point in the history
This fixes Bug #1152, where calling reconnect can cause endless connect/disconnect loop.
  • Loading branch information
Yoseph Maguire authored Oct 5, 2020
1 parent 66f7de7 commit 541f201
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ MqttClient.prototype.unsubscribe = function () {
*
* @returns {MqttClient} this - for chaining
* @param {Boolean} force - do not wait for all in-flight messages to be acked
* @param {Object} opts - added to the disconnect packet
* @param {Function} cb - called when the client has been closed
*
* @api public
Expand Down Expand Up @@ -929,8 +930,13 @@ MqttClient.prototype.reconnect = function (opts) {
MqttClient.prototype._reconnect = function () {
debug('_reconnect: emitting reconnect to client')
this.emit('reconnect')
debug('_reconnect: calling _setupStream')
this._setupStream()
if (this.connected) {
this.end(() => { this._setupStream() })
debug('client already connected. disconnecting first.')
} else {
debug('_reconnect: calling _setupStream')
this._setupStream()
}
}

/**
Expand Down

0 comments on commit 541f201

Please sign in to comment.