简体   繁体   English

如何在不使用 js 库的情况下使用 websocket 连接 mqtt?

[英]How to connect with mqtt using websocket without using js libraries?

I have a business requirement where I want to connect mqtt server(in my case using mosquito) without using available libraries like paho js or others.我有一个业务需求,我想在不使用 paho js 或其他可用库的情况下连接 mqtt 服务器(在我的情况下使用蚊子)。 Instead of using libraries I want to use simply我想简单地使用而不是使用库

var ws = new Websocket(maqtt_url);

After creating websocket connection I want to subscribe on a single topic and receive messages.创建 websocket 连接后,我想订阅单个主题并接收消息。 Subscription to topic can be hard coded.订阅主题可以硬编码。

Is this possible?这可能吗?

Sure it's possible, but your just going to end up re-writing all of the paho library yourself and including it in your application.当然这是可能的,但是您最终要自己重写所有 paho 库并将其包含在您的应用程序中。

If you really want to go down this path then the MQTT spec can be found here which will tell you the format of the如果你真的想走这条路,那么可以在这里找到 MQTT 规范它会告诉你

  • Connection Packet连接包
  • Subscription Packet订阅包
  • Message Packet消息包
  • Keep Alive Packet保持活动数据包

all of which you will need to implement in order to maintain a connection to the broker.您需要实现所有这些才能保持与代理的连接。

An mqtt over websockets example with encoding of the mqtt protocol data without using a library is available here .以上的WebSockets例如MQTT与不使用库MQTT协议数据的编码是可用在这里 It maybe a good starting point for developing applications.这可能是开发应用程序的一个很好的起点。

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

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