当前位置: 首页 > 知识库问答 >
问题:

windows下的MQTT+Mosquitto+JavaScript

步博涉
2023-03-14

我是新的在MQTT所以可以有人帮助我连接MQTT与蚊子使用javascript我正在使用这段代码,但它给错误······

<script type='text/javascript' src='jquery-1.10.1.js'></script>

<script type='text/javascript' src="mqttws31.js"></script>

    var client = new Messaging.Client("ns.testingindia.tld", 1883, "myclientid_" + parseInt(Math.random() * 100, 10));

     //Gets  called if the websocket/mqtt connection gets disconnected for any reason
     client.onConnectionLost = function (responseObject) {
         //Depending on your scenario you could implement a reconnect logic here
         alert("connection lost: " + responseObject.errorMessage);
     };

     //Gets called whenever you receive a message for your subscriptions
     client.onMessageArrived = function (message) {
         //Do something with the push message you received
         $('#messages').append('Topic: ' + message.destinationName + '  | ' + message.payloadString + '
'); }; //Connect Options var options = { timeout: 3, //Gets Called if the connection has sucessfully been established onSuccess: function () { alert("Connected"); }, //Gets Called if the connection could not be established onFailure: function (message) { document.write("Connection failed: " + message.errorMessage); alert("Connection failed: " + message.errorMessage); } }; //Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker var publish = function (payload, topic, qos) { //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations) var message = new Messaging.Message(payload); message.destinationName = topic; message.qos = qos; client.send(message); } //]]>

共有1个答案

董良策
2023-03-14

您正在连接到端口1883,这是默认的MQTT端口。我假设您的意思是使用Websockets,这通常是在不同的端口号上配置的。如果您使用的代理支持Websocket,请确保使用messaging.client()连接到正确的端口。

如果您使用的是Mosquitto代理,那么您需要bitbucket存储库中的1.4版本来支持Websocket,但是请注意,Mosquitto1.4还没有发布。

 类似资料:
  • 我试图在TLS支持下运行MQTT broker(Mosquitto)。我按照http://rockingdlabs.dunmire.org/exhibities/ssl-client-certs-to-secure-mqtt生成证书和配置。如果我跑 它在/var/log/mosquitto/mosquitto.log中抛出错误

  • 重新启动时,Mosquitto不可靠地转发排队消息(持久化不起作用) 运行蚊子版本:1.4.15 这是最后测试的配置:

  • 我的Mosquitto实例没有设置身份验证或密码。我尝试在Mosquitto日志中打开调试消息,但它们没有显示任何有用的信息。我不知所措。为什么我无法从C++Paho代码连接到Mosquitto? 编辑:这是客户端代码...同样,这对Adafruit很有效,但是当我把它指向我在localhost的蚊子时,它会像描述的那样挂起。(我省略了用户名和密码--我正在发送它们,但我真的不认为这些是问题所在,

  • Mosquitto是一个开源(BSD许可证)的消息代理,实现MQTT(消息队列遥测传输)协议版本3.1。 Mosquitto提供了Windows、Linux以及qnx系统的版本 MQTT是轻量级基于代理的发布/订阅的消息传输协议. 有三种消息发布服务质量: “至多一次”,消息发布完全依赖底层 TCP/IP 网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记录无所谓,因

  • 我一直在考虑MQTT协议,但我不想让一个外部服务器运行MQTT代理,而且我找不到Win IoT的代理。Windows IOT Core是否存在MQTT代理?如果没有,你会推荐什么通信协议?