运行 Paho javascript 示例会因为连接循环而导致网络连接崩溃



我正在尝试通过websockets和paho javascript客户端连接到本地mosquitto mqqt代理。但是,使用 https://www.eclipse.org/paho/clients/js/中显示的示例会使我的网络连接崩溃。在chrome或ie控制台中,我发现了问题:连接一遍又一遍(函数onConnect每秒调用多次(。

client.connect({onSuccess:onConnect});  
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("Connection was successful");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}

在代理上,我可以看到客户端正在连接:

New client connected from 192.168.1.3 as web_78 (c1, k60, u'user123').

但客户端不会广播该消息。当我终止浏览器时,我在客户端上看到:

Socket error on client web_78, disconnecting.

同样的事情发生,当我尝试不同的代码示例时,如下所示:https://jpmens.net/2014/07/03/the-mosquitto-mqtt-broker-gets-websockets-support/

我在我的树莓派上使用 mosquitto 版本 1.5,真的不知道如何让它运行。也许问题是 mosquitto 服务器而不是客户端?

这是我的mosquitto.conf的一部分,我在其中定义端口

listener 9001
protocol websockets

其他客户端可以连接(例如通过paho python(和mqtt(没有websockets(。

pi@raspberrypi ~ $ sudo mosquitto -c /etc/mosquitto/mosquitto.conf
1530009485: mosquitto version 1.5 starting
1530009485: Config loaded from /etc/mosquitto/mosquitto.conf.
1530009485: Opening websockets listen socket on port 9001.
1530009485: Opening ipv4 listen socket on port 1883.
1530009485: Opening ipv6 listen socket on port 1883.
1530009485: New connection from 192.168.1.51 on port 1883.
1530009485: New client connected from 192.168.1.51 as DVES_9CE05F (c1, k15, u'johann').

任何帮助将受到高度赞赏!

编辑更新:

问题一定出在我的服务器中,因为我可以访问像HiveMQ这样的公共代理。

我遇到了同样的问题。在Mosquitto 1.5.x中存在与libwebsockets相关的问题,至少在Raspberry Pi上安装的带有Raspbian jessie的版本中是这样。当我将Mosquitto降级到1.4.15时,这个问题为我解决了。

阅读有关它的信息,https://github.com/eclipse/mosquitto/issues/1050

最新更新