我正在尝试通过websockets和paho javascript客户端连接到本地mosquitto mqqt代理。但是,使用https://www.eclipse.org/paho/clients/js/中显示的示例会崩溃我的网络连接。在chrome或者控制台中我发现了问题:连接是反复进行的(每秒多次调用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.
同样的事情发生了,当我尝试不同的Codeexample像这样:https://jpmens.net/2014/07/03/the-mosquitto-mqtt-broker-gets-websockets-support/
我在我的Raspberry Pi上使用了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相关的问题,至少在使用Raspbian jessie的Raspberry Pi上安装的版本中存在问题。当我将Mosquitto降级到1.4.15时,这个问题就解决了。