From 3be75fa82256f76e7853cfb27178df3ac11aaf33 Mon Sep 17 00:00:00 2001 From: Phil Day Date: Thu, 5 May 2022 16:12:28 +0100 Subject: [PATCH] Add Force parameter mqtt client.end() when called in disconnect --- .../@node-red/nodes/core/network/10-mqtt.js | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js index 1e8f8ce9c..2478da4b2 100644 --- a/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js +++ b/packages/node_modules/@node-red/nodes/core/network/10-mqtt.js @@ -765,11 +765,23 @@ module.exports = function(RED) { } callback && typeof callback == "function" && callback(); }; - if(node.closing) { - return _callback(false, force); - } - var endCallBack = function endCallBack() { - } + if(!node.client) { return _callback(); } + if(node.closing) { return _callback(); } + + let waitEnd = (client, ms) => { + return new Promise( (resolve, reject) => { + node.closing = true; + if(!client) { + resolve(); + } else { + const t = setTimeout(reject, ms); + client.end(true, () => { + clearTimeout(t); + resolve() + }); + } + }); + }; if(node.connected && node.closeMessage) { node.publish(node.closeMessage, function (err) { node.client.end(endCallBack);