1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #4078 from flying7eleven/option-to-disable-mqtt-ubsubscribe-on-disconnect

Option to disable MQTT topic unsubscribe on disconnect
This commit is contained in:
Nick O'Leary 2023-03-20 20:32:08 +00:00 committed by GitHub
commit 4667e76c6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 14 deletions

View File

@ -249,6 +249,12 @@
<span id="node-config-input-cleansession-label" data-i18n="mqtt.label.cleansession"></span> <span id="node-config-input-cleansession-label" data-i18n="mqtt.label.cleansession"></span>
</label> </label>
</div> </div>
<div class="form-row mqtt-persistence">
<label for="node-config-input-autoUnsubscribe" style="width: auto;">
<input type="checkbox" id="node-config-input-autoUnsubscribe" style="position: relative;vertical-align: bottom; top: -2px; width: 15px;height: 15px;">
<span id="node-config-input-autoUnsubscribe-label" data-i18n="mqtt.label.autoUnsubscribe"></span>
</label>
</div>
<div class="form-row mqtt5"> <div class="form-row mqtt5">
<label style="width:auto" for="node-config-input-sessionExpiry"><span data-i18n="mqtt.label.sessionExpiry"></span></label> <label style="width:auto" for="node-config-input-sessionExpiry"><span data-i18n="mqtt.label.sessionExpiry"></span></label>
<input type="number" min="0" id="node-config-input-sessionExpiry" style="width: 100px" > <input type="number" min="0" id="node-config-input-sessionExpiry" style="width: 100px" >
@ -483,17 +489,23 @@
tls: {type:"tls-config",required: false, tls: {type:"tls-config",required: false,
label:RED._("node-red:mqtt.label.use-tls") }, label:RED._("node-red:mqtt.label.use-tls") },
clientid: {value:"", validate: function(v, opt) { clientid: {value:"", validate: function(v, opt) {
var ok = false; let ok = true;
if ($("#node-config-input-clientid").length) { if ($("#node-config-input-clientid").length) {
// Currently editing the node // Currently editing the node
ok = $("#node-config-input-cleansession").is(":checked") || (v||"").length > 0; let needClientId = !$("#node-config-input-cleansession").is(":checked") || !$("#node-config-input-autoUnsubscribe").is(":checked")
if (needClientId) {
ok = (v||"").length > 0;
}
} else { } else {
ok = (this.cleansession===undefined || this.cleansession) || (v||"").length > 0; let needClientId = !(this.cleansession===undefined || this.cleansession) || this.autoUnsubscribe;
if (needClientId) {
ok = (v||"").length > 0;
}
} }
if (ok) { if (!ok) {
return ok; return RED._("node-red:mqtt.errors.invalid-client-id");
} }
return RED._("node-red:mqtt.errors.invalid-client-id"); return true;
}}, }},
autoConnect: {value: true}, autoConnect: {value: true},
usetls: {value: false}, usetls: {value: false},
@ -505,6 +517,7 @@
label: RED._("node-red:mqtt.label.keepalive"), label: RED._("node-red:mqtt.label.keepalive"),
validate:RED.validators.number(false)}, validate:RED.validators.number(false)},
cleansession: {value: true}, cleansession: {value: true},
autoUnsubscribe: {value: true},
birthTopic: {value:"", validate:validateMQTTPublishTopic}, birthTopic: {value:"", validate:validateMQTTPublishTopic},
birthQos: {value:"0"}, birthQos: {value:"0"},
birthRetain: {value:"false"}, birthRetain: {value:"false"},
@ -620,6 +633,10 @@
this.cleansession = true; this.cleansession = true;
$("#node-config-input-cleansession").prop("checked",true); $("#node-config-input-cleansession").prop("checked",true);
} }
if (typeof this.autoUnsubscribe === 'undefined') {
this.autoUnsubscribe = true;
$("#node-config-input-autoUnsubscribe").prop("checked",true);
}
if (typeof this.usetls === 'undefined') { if (typeof this.usetls === 'undefined') {
this.usetls = false; this.usetls = false;
$("#node-config-input-usetls").prop("checked",false); $("#node-config-input-usetls").prop("checked",false);
@ -635,6 +652,14 @@
if (typeof this.protocolVersion === 'undefined') { if (typeof this.protocolVersion === 'undefined') {
this.protocolVersion = 4; this.protocolVersion = 4;
} }
$("#node-config-input-cleansession").on("change", function() {
const useCleanSession = $("#node-config-input-cleansession").is(':checked');
if(useCleanSession) {
$("div.form-row.mqtt-persistence").hide();
} else {
$("div.form-row.mqtt-persistence").show();
}
});
$("#node-config-input-protocolVersion").on("change", function() { $("#node-config-input-protocolVersion").on("change", function() {
var v5 = $("#node-config-input-protocolVersion").val() == "5"; var v5 = $("#node-config-input-protocolVersion").val() == "5";
if(v5) { if(v5) {

View File

@ -482,6 +482,7 @@ module.exports = function(RED) {
setIfHasProperty(opts, node, "protocolVersion", init); setIfHasProperty(opts, node, "protocolVersion", init);
setIfHasProperty(opts, node, "keepalive", init); setIfHasProperty(opts, node, "keepalive", init);
setIfHasProperty(opts, node, "cleansession", init); setIfHasProperty(opts, node, "cleansession", init);
setIfHasProperty(opts, node, "autoUnsubscribe", init);
setIfHasProperty(opts, node, "topicAliasMaximum", init); setIfHasProperty(opts, node, "topicAliasMaximum", init);
setIfHasProperty(opts, node, "maximumPacketSize", init); setIfHasProperty(opts, node, "maximumPacketSize", init);
setIfHasProperty(opts, node, "receiveMaximum", init); setIfHasProperty(opts, node, "receiveMaximum", init);
@ -590,6 +591,9 @@ module.exports = function(RED) {
if (typeof node.cleansession === 'undefined') { if (typeof node.cleansession === 'undefined') {
node.cleansession = true; node.cleansession = true;
} }
if (typeof node.autoUnsubscribe === 'undefined') {
node.autoUnsubscribe = true;
}
//use url or build a url from usetls://broker:port //use url or build a url from usetls://broker:port
if (node.url && node.brokerurl !== node.url) { if (node.url && node.brokerurl !== node.url) {
@ -660,6 +664,7 @@ module.exports = function(RED) {
node.options.password = node.password; node.options.password = node.password;
node.options.keepalive = node.keepalive; node.options.keepalive = node.keepalive;
node.options.clean = node.cleansession; node.options.clean = node.cleansession;
node.options.autoUnsubscribe = node.autoUnsubscribe;
node.options.clientId = node.clientid || 'nodered_' + RED.util.generateId(); node.options.clientId = node.clientid || 'nodered_' + RED.util.generateId();
node.options.reconnectPeriod = RED.settings.mqttReconnectTime||5000; node.options.reconnectPeriod = RED.settings.mqttReconnectTime||5000;
delete node.options.protocolId; //V4+ default delete node.options.protocolId; //V4+ default
@ -1228,12 +1233,16 @@ module.exports = function(RED) {
node.on('close', function(removed, done) { node.on('close', function(removed, done) {
if (node.brokerConn) { if (node.brokerConn) {
if(node.isDynamic) { if(node.isDynamic) {
Object.keys(node.dynamicSubs).forEach(function (topic) { if (node.brokerConn.options.autoUnsubscribe) {
node.brokerConn.unsubscribe(topic, node.id, removed); Object.keys(node.dynamicSubs).forEach(function (topic) {
}); node.brokerConn.unsubscribe(topic, node.id, removed);
node.dynamicSubs = {}; });
node.dynamicSubs = {};
}
} else { } else {
node.brokerConn.unsubscribe(node.topic,node.id, removed); if (node.brokerConn.options.autoUnsubscribe) {
node.brokerConn.unsubscribe(node.topic, node.id, removed);
}
} }
node.brokerConn.deregister(node, done, removed); node.brokerConn.deregister(node, done, removed);
node.brokerConn = null; node.brokerConn = null;

View File

@ -362,6 +362,7 @@
"port": "Port", "port": "Port",
"keepalive": "Keep-Alive", "keepalive": "Keep-Alive",
"cleansession": "Bereinigte Sitzung (clean session) verwenden", "cleansession": "Bereinigte Sitzung (clean session) verwenden",
"autoUnsubscribe": "Abonnement bei Verbindungsende automatisch beenden",
"cleanstart": "Verwende bereinigten Start", "cleanstart": "Verwende bereinigten Start",
"use-tls": "TLS", "use-tls": "TLS",
"tls-config": "TLS-Konfiguration", "tls-config": "TLS-Konfiguration",

View File

@ -414,6 +414,7 @@
"port": "Port", "port": "Port",
"keepalive": "Keep Alive", "keepalive": "Keep Alive",
"cleansession": "Use clean session", "cleansession": "Use clean session",
"autoUnsubscribe": "Automatically unsubscribe when disconnecting",
"cleanstart": "Use clean start", "cleanstart": "Use clean start",
"use-tls": "Use TLS", "use-tls": "Use TLS",
"tls-config": "TLS Configuration", "tls-config": "TLS Configuration",

View File

@ -53,7 +53,7 @@ describe('MQTT Nodes', function () {
mqttBroker.should.have.property('broker', BROKER_HOST); mqttBroker.should.have.property('broker', BROKER_HOST);
mqttBroker.should.have.property('port', BROKER_PORT); mqttBroker.should.have.property('port', BROKER_PORT);
mqttBroker.should.have.property('brokerurl'); mqttBroker.should.have.property('brokerurl');
// mqttBroker.should.have.property('autoUnsubscribe', true);//default: true mqttBroker.should.have.property('autoUnsubscribe', true); //default: true
mqttBroker.should.have.property('autoConnect', false);//Set "autoConnect:false" in brokerOptions mqttBroker.should.have.property('autoConnect', false);//Set "autoConnect:false" in brokerOptions
mqttBroker.should.have.property('options'); mqttBroker.should.have.property('options');
mqttBroker.options.should.have.property('clean', true); mqttBroker.options.should.have.property('clean', true);
@ -96,8 +96,8 @@ describe('MQTT Nodes', function () {
mqttBroker.should.have.property('broker', BROKER_HOST); mqttBroker.should.have.property('broker', BROKER_HOST);
mqttBroker.should.have.property('port', BROKER_PORT); mqttBroker.should.have.property('port', BROKER_PORT);
mqttBroker.should.have.property('brokerurl'); mqttBroker.should.have.property('brokerurl');
// mqttBroker.should.have.property('autoUnsubscribe', true);//default: true mqttBroker.should.have.property('autoUnsubscribe', true);
mqttBroker.should.have.property('autoConnect', false);//Set "autoConnect:false" in brokerOptions mqttBroker.should.have.property('autoConnect', false); //Set "autoConnect:false" in brokerOptions
mqttBroker.should.have.property('options'); mqttBroker.should.have.property('options');
mqttBroker.options.should.have.property('clean', false); mqttBroker.options.should.have.property('clean', false);
mqttBroker.options.should.have.property('clientId', 'clientid'); mqttBroker.options.should.have.property('clientId', 'clientid');