【问题标题】:Unable to connect to mqtt broker using javascript client无法使用 javascript 客户端连接到 mqtt 代理
【发布时间】:2015-07-01 17:31:04
【问题描述】:

我收到了错误

SCRIPT12152:WebSocket 错误:网络错误 12152,服务器返回无效或无法识别的响应

在 IE 中,和

与“ws://192.168.1.100:1883/”的 WebSocket 连接失败:在收到握手响应之前连接已关闭

在 chrome.. 下面是我用过的一段代码

<!DOCTYPE html>
<html lang="en">

<head></head>

<body>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../js/mqttws31.js"></script>
<script src="../js/mqttws31-min.js"></script>
<script src="../js/reconnecting-websocket.js"></script>
<script src="../js/reconnecting-websocket.min.js"></script>
<script>

// Create a client instance
client = new Paho.MQTT.Client("192.168.1.100", 1883, "100");
var s = new ReconnectingWebSocket("ws://192.168.1.100:1883");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;

// connect the client
client.connect({onSuccess:onConnect});

// called when the client connects
function onConnect() {
    alert("connected");
    // Once a connection has been made, make a subscription and send       
    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>

【问题讨论】:

  • 您使用的是什么代理?是支持 websockets 的吗?
  • 我尝试使用 Mosquitto 代理然后我遇到了问题,然后我用 HiveMQ 代理替换了,我只是启用了 websocket 并修复了它但问题是我阅读了 mosquitto 1.4 以上支持我制作的 websocket蚊子经纪人也有同样的变化,但仍然下降
  • 您仍然连接到错误的端口。在另一个问题中,我们为 mosquitto 设置的 websocket 端口是 1884 而不是 1883
  • 使用 HiveMQ 代理,我确实将 websocket 的侦听器端口设置为 8000,现在它工作正常。抱歉没有添加此信息,我也删除了重新连接代码
  • 请帮我处理 Mosquitto 经纪人

标签: windows websocket mqtt mosquitto hivemq


【解决方案1】:

如果您使用 mosquitto 提供的 Windows 二进制文件,您应该知道它们没有启用 libwebsockets 支持。如果你想在 Windows 上使用 mosquitto 支持 websockets,你需要自己编译 libwebsockets,然后在启用 websockets 支持后编译 mosquitto。

另外值得注意的是,目前 Windows 上的 libwebsockets 支持不是很好,特别是连接的客户端数量限制为 64 个。

【讨论】:

  • 我还有其他选择可以使用 mosquitto 代理作为 javascript 客户端吗?
  • 我不确定你在问什么。 mosquitto 代理不充当 javascript 客户端。您可以使用任何带有 mosquitto 的 javascript MQTT 客户端,但您必须在 Windows 上自行编译才能获得 websockets 支持。
  • 在java中我使用tcp for mqtt ..这里使用javascript我应该只依赖websocket还是使用tcp连接的任何方式?抱歉,我对 tcs/websocket 等网络东西不熟悉 :(
  • Websockets 在 tcp 之上工作。当然,您也可以实现一个使用直接套接字的 javascript 客户端。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2019-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 1970-01-01
  • 2022-06-19
相关资源
最近更新 更多