mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Fix MQTT client reconnect logic
This commit is contained in:
parent
775297d625
commit
0b7fa1ab5c
@ -45,6 +45,7 @@ function MQTTClient(port,host) {
|
|||||||
util.inherits(MQTTClient, events.EventEmitter);
|
util.inherits(MQTTClient, events.EventEmitter);
|
||||||
|
|
||||||
MQTTClient.prototype.connect = function(options) {
|
MQTTClient.prototype.connect = function(options) {
|
||||||
|
if (!this.connected) {
|
||||||
var self = this;
|
var self = this;
|
||||||
options = options||{};
|
options = options||{};
|
||||||
self.options = options;
|
self.options = options;
|
||||||
@ -180,6 +181,7 @@ MQTTClient.prototype.connect = function(options) {
|
|||||||
this.connectionError = false;
|
this.connectionError = false;
|
||||||
client.connect(self.options);
|
client.connect(self.options);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTClient.prototype.subscribe = function(topic,qos) {
|
MQTTClient.prototype.subscribe = function(topic,qos) {
|
||||||
|
@ -91,7 +91,6 @@ module.exports = {
|
|||||||
client.on('connect',function() {
|
client.on('connect',function() {
|
||||||
if (client) {
|
if (client) {
|
||||||
util.log('[mqtt] ['+uid+'] connected to broker tcp://'+broker+':'+port);
|
util.log('[mqtt] ['+uid+'] connected to broker tcp://'+broker+':'+port);
|
||||||
|
|
||||||
connecting = false;
|
connecting = false;
|
||||||
for (var s in subscriptions) {
|
for (var s in subscriptions) {
|
||||||
var topic = subscriptions[s].topic;
|
var topic = subscriptions[s].topic;
|
||||||
@ -109,13 +108,13 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
client.on('connectionlost', function(err) {
|
client.on('connectionlost', function(err) {
|
||||||
util.log('[mqtt] ['+uid+'] connection lost to broker tcp://'+broker+':'+port);
|
util.log('[mqtt] ['+uid+'] connection lost to broker tcp://'+broker+':'+port);
|
||||||
|
connecting = false;
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
if (client) {
|
obj.connect();
|
||||||
client.connect(options);
|
|
||||||
}
|
|
||||||
}, settings.mqttReconnectTime||5000);
|
}, settings.mqttReconnectTime||5000);
|
||||||
});
|
});
|
||||||
client.on('disconnect', function() {
|
client.on('disconnect', function() {
|
||||||
|
connecting = false;
|
||||||
util.log('[mqtt] ['+uid+'] disconnected from broker tcp://'+broker+':'+port);
|
util.log('[mqtt] ['+uid+'] disconnected from broker tcp://'+broker+':'+port);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user