From 15b9224044e80fcc9eaa4a539e7dcd5c960bb5f7 Mon Sep 17 00:00:00 2001 From: Heiko Henning UE85540 Date: Mon, 9 Sep 2019 10:39:37 +0200 Subject: [PATCH] Fix reconnecting if a list of hosts|uris was given. At the seconds+ failover --- src/paho-mqtt.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/paho-mqtt.js b/src/paho-mqtt.js index d8c78cb..c9a622e 100644 --- a/src/paho-mqtt.js +++ b/src/paho-mqtt.js @@ -1055,6 +1055,7 @@ function onMessageArrived(message) { this.connected = false; + this._trace("_doConnect ", wsurl); if (this.connectOptions.mqttVersion < 4) { this.socket = new global.WebSocket(wsurl, ["mqttv3.1"]); @@ -1542,8 +1543,12 @@ function onMessageArrived(message) { if (this._reconnectInterval < 128) this._reconnectInterval = this._reconnectInterval * 2; if (this.connectOptions.uris) { - this.hostIndex = 0; - this._doConnect(this.connectOptions.uris[0]); + // Loop through all host until we find an working one. + this.hostIndex++; + if (this.hostIndex >= this.connectOptions.uris.length) { + this.hostIndex = 0; + } + this._doConnect(this.connectOptions.uris[this.hostIndex]); } else { this._doConnect(this.uri); }