尝试使用 Mqtt.js 通过 WebSocket 连接到 Mosquitto MQTT
我想构建一个网页来发送和接收主题。我正在使用 mosquitto 和 MQTT.js。
- 我使用它作为配置文件:
协议 websocket 听众 1884
- 我运行 sudo mosquitto -c /etc/mosquitto/mosquitto.conf 并得到
1647529861:mosquitto 版本 2.0.14 开始 1647529861:从 /etc/mosquitto/mosquitto.conf 加载配置。 1647529861:在端口 1884 上打开 websockets 侦听套接字。 1647529861:mosquitto 版本 2.0.14 正在运行
- 我正在使用一个简单的
mqtt.html
(完整源代码):; <脚本> const client = mqtt.connect("ws://localhost:1884")
问题:
- 在 Chrome 中,它只是与 101 连接并一遍又一遍地执行。当您检查 websockets 消息时,“服务器”收到:
00000000: 2002 0005
。当我检查 mosquitto 的日志时:我收到很多1647530429: Client mqttjs_b0e54ea4 Closed its connection.
我正在运行:
LSB Version: n/a
Distributor ID: ManjaroLinux
Description: Manjaro Linux
Release: 21.2.5
Codename: Qonos
I want to build a web page to send and receive topics. I'm using mosquitto and MQTT.js.
- I'm using this as an config file:
protocol websockets listener 1884
- I run
sudo mosquitto -c /etc/mosquitto/mosquitto.conf
and get1647529861: mosquitto version 2.0.14 starting 1647529861: Config loaded from /etc/mosquitto/mosquitto.conf. 1647529861: Opening websockets listen socket on port 1884. 1647529861: mosquitto version 2.0.14 running
- I'm using a simple
mqtt.html
(full source):<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script> <script> const client = mqtt.connect("ws://localhost:1884") </script>
Problem:
- In Chrome it just connects with 101 and does it again and again. When you check the websockets messages, the "server" receives:
00000000: 2002 0005
. When I check the log of mosquitto: I get a lot of1647530429: Client mqttjs_b0e54ea4 closed its connection.
I'm running on:
LSB Version: n/a
Distributor ID: ManjaroLinux
Description: Manjaro Linux
Release: 21.2.5
Codename: Qonos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
allow_annonymous true
添加到您的配置文件中。mosquitto 2.x 的默认设置是不允许来自未经身份验证的客户端的连接。
您还应该颠倒配置中的行,
protocol
选项仅适用于文件中按顺序排列的最后一个listener
。您所拥有的将 websocket 协议应用于端口 1883 上的默认侦听器。
You need to add
allow_annonymous true
to your config file.The default for mosquitto 2.x is to not allow connections from unauthenticated clients.
You should also reverse the lines in your config,
The
protocol
option only applies to the lastlistener
in order down the file.What you have will apply the websocket protocol to the default listener on port 1883.