简体   繁体   English

使用纯 JavaScript 通过 Websocket 连接到 MQTT 代理

[英]Connecting to an MQTT Broker over Websocket using pure JavaScript

I have been using the native JS websocket object to build apps that use WS to communicate with the server.我一直在使用原生 JS websocket object 来构建使用 WS 与服务器通信的应用程序。 I have set up a Mosquitto MQTT broker and have enabled ws connections on the server.我已经设置了一个 Mosquitto MQTT 代理并在服务器上启用了 ws 连接。

I'm not a big fan of importing third-party libraries if it can be avoided but can't seem to find any examples of how to connect just using the native JS websocket object.如果可以避免,我不是导入第三方库的忠实拥护者,但似乎找不到任何仅使用本机 JS websocket object 进行连接的示例。

Does this mean it's not possible?这是否意味着不可能?

It's perfectly possible, but you will just end up totally re-implementing the whole MQTT protocol ( spec ).这是完全可能的,但您最终将完全重新实现整个 MQTT 协议( 规范)。 MQTT over Websockets is just the raw TCP protocol tunnelled over a websocket connection. Websockets 上的 MQTT 只是通过 websocket 连接隧道传输的原始 TCP 协议。

Just use the Paho Javascript client and save yourself a lot of effort and testing.只需使用Paho Javascript客户端,即可节省大量精力和测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM