简体   繁体   English

Javascript Paho MQTT 客户端不会停止接收回调

[英]Javascript Paho MQTT client won't stop receiving callbacks

I'm using the Paho MQTT client to connect to an MQTT server.我正在使用 Paho MQTT 客户端连接到 MQTT 服务器。 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. Id=pahoClient 的元素包含 Paho 客户端的 javascript 代码。 Disconnecting and removing it should stop the onMessageArrived event from firing but it doesn't.断开连接并删除它应该会阻止 onMessageArrived 事件触发,但它不会。 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.如果我再次运行我的代码,它将重新加载 Paho 客户端脚本并为每个 MQTT 主题更改接收 2 条消息。 Run again: 3,4,5 and so on.再次运行:3、4、5 等等。

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.背景信息:我正在谈论的 javascript 代码在“Wick-editor”中运行,这是一个运行 Javascript 的在线 Flash 克隆。 I'm connecting to the public MQTT test-server at: mqtt.eclipseprojects.io我正在连接到公共 MQTT 测试服务器:mqtt.eclipseprojects.io

The line containing window.MQTTClient.disconnect;包含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();试试: window.MQTTClient.disconnect();

eg例如

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 ).如果这没有帮助,请提供一个最小的可重现示例(目前我们看不到还有什么正在使用window.MQTTClient )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM