mirror of
https://github.com/louislam/uptime-kuma.git
synced 2024-12-28 15:09:42 -08:00
added cleartimeout in case client is already ended
This commit is contained in:
parent
22256dfcd2
commit
0345719e53
|
@ -99,6 +99,12 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
|
||||||
hostname = "mqtt://" + hostname;
|
hostname = "mqtt://" + hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeoutID = setTimeout(() => {
|
||||||
|
debug("MQTT timeout triggered");
|
||||||
|
client.end();
|
||||||
|
reject("Timeout");
|
||||||
|
}, interval * 1000);
|
||||||
|
|
||||||
debug("MQTT connecting");
|
debug("MQTT connecting");
|
||||||
|
|
||||||
let client = mqtt.connect(hostname, {
|
let client = mqtt.connect(hostname, {
|
||||||
|
@ -114,26 +120,22 @@ exports.mqttAsync = function (hostname, topic, okMessage, options = {}) {
|
||||||
|
|
||||||
client.on("error", (error) => {
|
client.on("error", (error) => {
|
||||||
client.end();
|
client.end();
|
||||||
|
clearTimeout(timeoutID);
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("message", (messageTopic, message) => {
|
client.on("message", (messageTopic, message) => {
|
||||||
if (messageTopic == topic) {
|
if (messageTopic == topic) {
|
||||||
|
client.end();
|
||||||
|
clearTimeout(timeoutID);
|
||||||
if (message.toString() === okMessage) {
|
if (message.toString() === okMessage) {
|
||||||
client.end();
|
|
||||||
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
|
resolve(`Topic: ${messageTopic}; Message: ${message.toString()}`);
|
||||||
} else {
|
} else {
|
||||||
client.end();
|
|
||||||
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
|
reject(new Error(`Error; Topic: ${messageTopic}; Message: ${message.toString()}`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
client.end();
|
|
||||||
reject("Timeout");
|
|
||||||
}, interval * 1000);
|
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue