Don't force reconnect mqtt client if message arrives

Fixes the annoying mqtt connect/disconnect cycle
This commit is contained in:
Nick O'Leary 2016-02-19 22:52:43 +00:00
parent 3b44d9972e
commit c6ad2c9ad2
2 changed files with 26 additions and 22 deletions

View File

@ -27,9 +27,15 @@
</script>
<script type="text/x-red" data-help-name="mqtt in">
<p>MQTT input node. Connects to a broker and subscribes to the specified topic. The topic may contain MQTT wildcards.</p>
<p>Outputs an object called <code>msg</code> containing <code>msg.topic</code>, <code>msg.payload</code>, <code>msg.qos</code> and <code>msg.retain</code>.</p>
<p><code>msg.payload</code> is usually a string, but can also be a binary buffer.</p>
<p>Connects to a broker and subscribes to the specified topic.</p>
<p>Outputs a message with the properties:</p>
<ul>
<li><code>msg.topic</code></li>
<li><code>msg.payload</code></li>
<li><code>msg.qos</code></li>
<li><code>msg.retain</code></li>
</ul>
<p><code>msg.payload</code> will be a String, unless it is detected as a binary buffer.</p>
</script>
<script type="text/javascript">
@ -84,10 +90,14 @@
</script>
<script type="text/x-red" data-help-name="mqtt out">
<p>Connects to a MQTT broker and publishes <code>msg.payload</code> either to the <code>msg.topic</code> or to the topic specified in the edit window. The value in the edit window has precedence.</p>
<p>Likewise QoS and/or retain values in the edit panel will overwrite any <code>msg.qos</code> and <code>msg.retain</code> properties. If nothing is set they default to <i>0</i> and <i>false</i> respectively.</p>
<p>If <code>msg.payload</code> contains an object it will be stringified before being sent.</p>
<p><code>msg.payload</code> can be a binary Buffer.</p>
<p>Connects to a MQTT broker and publishes messages.</p>
<p>The topic used can be configured in the node or, if left blank, can be set
by <code>msg.topic</code>.</p>
<p>Likewise the QoS and retain values can be configured in the node or, if left
blank, set by <code>msg.qos</code> and <code>msg.retain</code> respectively.
By default, messages are published at QoS 0 with the retain flag set to false.</p>
<p>If <code>msg.payload</code> contains an object it will be converted to JSON
before being sent.</p>
</script>
<script type="text/javascript">
@ -227,7 +237,7 @@
usetls: {value: false},
verifyservercert: { value: false},
compatmode: { value: true},
keepalive: {value:15,validate:RED.validators.number()},
keepalive: {value:60,validate:RED.validators.number()},
cleansession: {value: true},
willTopic: {value:""},
willQos: {value:"0"},

View File

@ -76,7 +76,7 @@ module.exports = function(RED) {
this.verifyservercert = false;
}
if (typeof this.keepalive === 'undefined'){
this.keepalive = 15;
this.keepalive = 60;
} else if (typeof this.keepalive === 'string') {
this.keepalive = Number(this.keepalive);
}
@ -184,15 +184,14 @@ module.exports = function(RED) {
if (node.birthMessage) {
node.publish(node.birthMessage);
}
// Send any queued messages
while(node.queue.length) {
var msg = node.queue.shift();
//console.log(msg);
node.publish(msg);
}
});
node.client.on("reconnect", function() {
for (var id in node.users) {
if (node.users.hasOwnProperty(id)) {
node.users[id].status({fill:"yellow",shape:"ring",text:"common.status.connecting"});
}
}
})
// Register disconnect handlers
node.client.on('close', function () {
if (node.connected) {
@ -272,11 +271,6 @@ module.exports = function(RED) {
retain: msg.retain || false
};
node.client.publish(msg.topic, msg.payload, options, function (err){return});
} else {
if (!node.connecting) {
node.connect();
}
node.queue.push(msg);
}
};