mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Track which event handlers we add to the mqtt client so we can removed them cleanly
This commit is contained in:
parent
b2ec040a8d
commit
7845ebffc5
@ -86,7 +86,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"dompurify": "2.3.6",
|
||||
"grunt": "1.5.2",
|
||||
"grunt": "^1.5.2",
|
||||
"grunt-chmod": "~1.1.1",
|
||||
"grunt-cli": "~1.4.3",
|
||||
"grunt-concurrent": "3.0.0",
|
||||
|
@ -453,6 +453,7 @@ module.exports = function(RED) {
|
||||
node.options = {};
|
||||
node.queue = [];
|
||||
node.subscriptions = {};
|
||||
node.clientListeners = []
|
||||
/** @type {mqtt.MqttClient}*/ this.client;
|
||||
node.setOptions = function(opts, init) {
|
||||
if(!opts || typeof opts !== "object") {
|
||||
@ -720,9 +721,9 @@ module.exports = function(RED) {
|
||||
node.serverProperties = {};
|
||||
node.client = mqtt.connect(node.brokerurl, node.options);
|
||||
node.client.setMaxListeners(0);
|
||||
let callbackDone = false; //prevent re-connects causing node.client.on('connect' firing callback multiple times
|
||||
let callbackDone = false; //prevent re-connects causing node._clientOn('connect' firing callback multiple times
|
||||
// Register successful connect or reconnect handler
|
||||
node.client.on('connect', function (connack) {
|
||||
node._clientOn('connect', function (connack) {
|
||||
node.closing = false;
|
||||
node.connecting = false;
|
||||
node.connected = true;
|
||||
@ -754,7 +755,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
setStatusConnected(node, true);
|
||||
// Remove any existing listeners before resubscribing to avoid duplicates in the event of a re-connection
|
||||
node.client.removeAllListeners('message');
|
||||
node._clientRemoveAllListeners('message');
|
||||
|
||||
// Re-subscribe to stored topics
|
||||
for (var s in node.subscriptions) {
|
||||
@ -766,7 +767,7 @@ module.exports = function(RED) {
|
||||
if (node.subscriptions[s].hasOwnProperty(r)) {
|
||||
qos = Math.max(qos,node.subscriptions[s][r].qos);
|
||||
_options = node.subscriptions[s][r].options;
|
||||
node.client.on('message',node.subscriptions[s][r].handler);
|
||||
node._clientOn('message',node.subscriptions[s][r].handler);
|
||||
}
|
||||
}
|
||||
_options.qos = _options.qos || qos;
|
||||
@ -779,11 +780,11 @@ module.exports = function(RED) {
|
||||
node.publish(node.birthMessage);
|
||||
}
|
||||
});
|
||||
node.client.on("reconnect", function() {
|
||||
node._clientOn("reconnect", function() {
|
||||
setStatusConnecting(node, true);
|
||||
});
|
||||
//Broker Disconnect - V5 event
|
||||
node.client.on("disconnect", function(packet) {
|
||||
node._clientOn("disconnect", function(packet) {
|
||||
//Emitted after receiving disconnect packet from broker. MQTT 5.0 feature.
|
||||
const rc = (packet && packet.properties && packet.reasonCode) || packet.reasonCode;
|
||||
const rs = packet && packet.properties && packet.properties.reasonString || "";
|
||||
@ -797,7 +798,7 @@ module.exports = function(RED) {
|
||||
setStatusDisconnected(node, true);
|
||||
});
|
||||
// Register disconnect handlers
|
||||
node.client.on('close', function () {
|
||||
node._clientOn('close', function () {
|
||||
if (node.connected) {
|
||||
node.connected = false;
|
||||
node.log(RED._("mqtt.state.disconnected",{broker:(node.clientid?node.clientid+"@":"")+node.brokerurl}));
|
||||
@ -809,7 +810,7 @@ module.exports = function(RED) {
|
||||
|
||||
// Register connect error handler
|
||||
// The client's own reconnect logic will take care of errors
|
||||
node.client.on('error', function (error) {
|
||||
node._clientOn('error', function (error) {
|
||||
});
|
||||
}catch(err) {
|
||||
console.log(err);
|
||||
@ -822,7 +823,7 @@ module.exports = function(RED) {
|
||||
if(node.connected || node.connecting) {
|
||||
setStatusDisconnected(node, true);
|
||||
}
|
||||
if(node.client) { node.client.removeAllListeners(); }
|
||||
if(node.client) { node._clientRemoveAllListeners(); }
|
||||
node.connecting = false;
|
||||
node.connected = false;
|
||||
callback && typeof callback == "function" && callback();
|
||||
@ -837,7 +838,7 @@ module.exports = function(RED) {
|
||||
resolve();
|
||||
} else {
|
||||
const t = setTimeout(reject, ms);
|
||||
client.end(true, () => {
|
||||
client.end(() => {
|
||||
clearTimeout(t);
|
||||
resolve()
|
||||
});
|
||||
@ -894,7 +895,7 @@ module.exports = function(RED) {
|
||||
};
|
||||
node.subscriptions[topic][ref] = sub;
|
||||
if (node.connected) {
|
||||
node.client.on('message',sub.handler);
|
||||
node._clientOn('message',sub.handler);
|
||||
node.client.subscribe(topic, options);
|
||||
}
|
||||
};
|
||||
@ -996,12 +997,31 @@ module.exports = function(RED) {
|
||||
node.on('close', function(done) {
|
||||
node.disconnect(function() {
|
||||
if(node.client) {
|
||||
node.client.removeAllListeners();
|
||||
node._clientRemoveAllListeners();
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
// Helper functions to track the event listners we add to the
|
||||
// client. The mqtt client also uses it own set of listeners
|
||||
// so we can't use removeAllListeners() wothout breaking it
|
||||
node._clientOn = function(event, f) {
|
||||
node.clientListeners.push({event, f})
|
||||
node.client.on(event, f)
|
||||
}
|
||||
|
||||
node._clientRemoveAllListeners = function(event) {
|
||||
node.clientListeners = node.clientListeners.filter((l) => {
|
||||
if (!event || (event == l.e)) {
|
||||
node.client.removeListener(l.event, l.f)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
return true;
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RED.nodes.registerType("mqtt-broker",MQTTBrokerNode,{
|
||||
|
Loading…
Reference in New Issue
Block a user