Reputation: 430
I'm using the Paho MQTT client to connect to an MQTT server. After I'm done receiving messages I run this bit of code:
function dummy() {
console.log("dummy");
}
if (window.MQTTClient!==null){
console.log("Disconnecting from MQTT server...");
window.MQTTClient.disconnect;
window.MQTTClient.onMessageArrived=dummy;
let element = document.getElementById("pahoClient");
console.log(element);
element.parentElement.removeChild(element);
window.MQTTClient=null;
}
The element with Id=pahoClient contains the javascript code for the Paho client. Disconnecting and removing it should stop the onMessageArrived event from firing but it doesn't. After the code is done running, the dummy function still gets called. I cannot make it stop.
If I run my code again it will reload the Paho client script and receive 2 messages for each MQTT topic change. Run again: 3,4,5 and so on.
Only after refreshing the page I get rid of the extra callbacks, but I don't want that.
Background information: The javascript code I'm talking about is running in "Wick-editor" an online Flash-clone that runs Javascript. I'm connecting to the public MQTT test-server at: mqtt.eclipseprojects.io
Upvotes: 0
Views: 215
Reputation: 18370
The line containing window.MQTTClient.disconnect;
will not do anything. To call a function you need to provide a parameter list (which can be empty).
Try: window.MQTTClient.disconnect();
e.g.
function test() {
console.log("foo1");
}
function test2() {
console.log("foo2");
}
test; // This will not do anything
test2(); // Will log "foo2"
If this does not help please provide a minimal reproducible example (currently we cannot see what else is using window.MQTTClient
).
Upvotes: 2