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