WebSocket错误:网络错误12031,与服务器的连接已重置
我想从UI订阅mqtt消息。我使用以下代码。我有mosquitto代理在我的机器上运行,所以我已经将url作为我的IP并且正在侦听端口号1883,我已经给出了一些随机的客户端ID
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/jquery/dist/jquery.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31-min.js"></script>
<script src="../paho.javascript-1.0.1/mqttws31.js"></script>
<script src="../js/browserMqtt.js"></script>
<script>
// Create a client instance
client = new Paho.MQTT.Client("10.9.177.110", 1883, "100");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
console.log("onConnect");
client.subscribe("/World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "/World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
</script>
</body>
</html>
答案 0 :(得分:0)
您能否确认您运行的是启用了WebSockets的Mosquitto版本?
答案 1 :(得分:0)
您尝试连接的是什么类型的经纪人?除了IBM MessageSight设备之外,我不知道任何其他可以通过Websockets为本机MQTT和MQTT共享相同端口的代理。
由于端口1883传统上用于本机MQTT,您是否记得在Websockets上为MQTT添加新的侦听器?
假设您使用的是mosquitto 1.4.x,那么您需要在配置文件中添加以下内容:
listerner 1884
protocol websockets
这将在端口1884上添加Websocket侦听器