Skip to content

Commit

Permalink
make sure MQTT client closes if redeploy during reconnect
Browse files Browse the repository at this point in the history
to close #1193
Thanks @tedhuang for the excellent problem determination
  • Loading branch information
Dave Conway-Jones committed Mar 10, 2017
1 parent 36ab16c commit 834e894
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions nodes/core/io/10-mqtt.html
Expand Up @@ -35,7 +35,7 @@
</script>

<script type="text/x-red" data-help-name="mqtt in">
<p>Connects to a broker and subscribes to the specified topic.</p>
<p>Connects to a MQTT broker and subscribes to the specified topic.</p>
<p>Outputs a message with the properties:</p>
<ul>
<li><code>msg.topic</code></li>
Expand Down Expand Up @@ -107,6 +107,7 @@
<p>Connects to a MQTT broker and publishes messages.</p>
<p><code>msg.payload</code> is used as the payload of the published message.
If it contains an Object it will be converted to JSON before being sent.
If it contains a binary Buffer the message will be published as-is.
</p>
<p>The topic used can be configured in the node or, if left blank, can be set
by <code>msg.topic</code>.</p>
Expand Down Expand Up @@ -147,9 +148,9 @@
<div id="mqtt-broker-tab-connection" style="display:none">
<div class="form-row node-input-broker">
<label for="node-config-input-broker"><i class="fa fa-globe"></i> <span data-i18n="mqtt.label.broker"></span></label>
<input class="input-append-left" type="text" id="node-config-input-broker" placeholder="e.g. localhost" style="width: 40%;" >
<label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> <span data-i18n="mqtt.label.port"></span></label>
<input type="text" id="node-config-input-port" data-i18n="[placeholder]mqtt.label.port" style="width:45px">
<input class="input-append-left" type="text" id="node-config-input-broker" placeholder="e.g. localhost" style="width:40%;" >
<label for="node-config-input-port" style="margin-left:20px; width:35px; "> <span data-i18n="mqtt.label.port"></span></label>
<input type="text" id="node-config-input-port" data-i18n="[placeholder]mqtt.label.port" style="width:55px">
</div>
<div class="form-row">
<input type="checkbox" id="node-config-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
Expand Down Expand Up @@ -299,7 +300,7 @@
id: "mqtt-broker-tab-will",
label: this._("mqtt.tabs-label.will")
});
setTimeout(function() { tabs.resize()},0);
setTimeout(function() { tabs.resize(); },0);
if (typeof this.cleansession === 'undefined') {
this.cleansession = true;
$("#node-config-input-cleansession").prop("checked",true);
Expand Down
20 changes: 10 additions & 10 deletions nodes/core/io/10-mqtt.js
Expand Up @@ -66,16 +66,16 @@ module.exports = function(RED) {

// If the config node is missing certain options (it was probably deployed prior to an update to the node code),
// select/generate sensible options for the new fields
if (typeof this.usetls === 'undefined'){
if (typeof this.usetls === 'undefined') {
this.usetls = false;
}
if (typeof this.compatmode === 'undefined'){
if (typeof this.compatmode === 'undefined') {
this.compatmode = true;
}
if (typeof this.verifyservercert === 'undefined'){
if (typeof this.verifyservercert === 'undefined') {
this.verifyservercert = false;
}
if (typeof this.keepalive === 'undefined'){
if (typeof this.keepalive === 'undefined') {
this.keepalive = 60;
} else if (typeof this.keepalive === 'string') {
this.keepalive = Number(this.keepalive);
Expand Down Expand Up @@ -110,7 +110,7 @@ module.exports = function(RED) {
this.options.keepalive = this.keepalive;
this.options.clean = this.cleansession;
this.options.reconnectPeriod = RED.settings.mqttReconnectTime||5000;
if (this.compatmode == "true" || this.compatmode === true){
if (this.compatmode == "true" || this.compatmode === true) {
this.options.protocolId = 'MQIsdp';
this.options.protocolVersion = 3;
}
Expand Down Expand Up @@ -140,14 +140,14 @@ module.exports = function(RED) {
var node = this;
this.users = {};

this.register = function(mqttNode){
this.register = function(mqttNode) {
node.users[mqttNode.id] = mqttNode;
if (Object.keys(node.users).length === 1) {
node.connect();
}
};

this.deregister = function(mqttNode,done){
this.deregister = function(mqttNode,done) {
delete node.users[mqttNode.id];
if (node.closing) {
return done();
Expand Down Expand Up @@ -266,7 +266,7 @@ module.exports = function(RED) {
}
if (Object.keys(sub).length === 0) {
delete node.subscriptions[topic];
if (node.connected){
if (node.connected) {
node.client.unsubscribe(topic);
}
}
Expand All @@ -287,7 +287,7 @@ module.exports = function(RED) {
qos: msg.qos || 0,
retain: msg.retain || false
};
node.client.publish(msg.topic, msg.payload, options, function (err){return});
node.client.publish(msg.topic, msg.payload, options, function(err) {return});
}
};

Expand All @@ -298,7 +298,7 @@ module.exports = function(RED) {
done();
});
this.client.end();
} else if (this.connecting) {
} else if (this.connecting || node.client.reconnecting) {
node.client.end();
done();
} else {
Expand Down

0 comments on commit 834e894

Please sign in to comment.