MQTT + Mosquitto + Javascript语言,窗口

问题描述 投票:0回答:3

我在MQTT新,所以有人可以帮助我使用JavaScript和Mosquitto连接MQTT我使用这个代码,但它给错误...

连接失败:AMQJS0007E套接字错误:不确定。

我的代码是:

<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); } //]]>
mqtt mosquitto
3个回答
2
投票

您正在连接到端口1883年这是默认MQTT端口。我假设你的意思是使用WebSockets,而且通常会在不同的端口号进行配置。如果您使用的经纪人WebSocket的支持,确保您Messaging.Client()连接到正确的端口。

如果您使用的Mosquitto经纪人,你会从它的bitbucket repository对WebSocket的支持,需要1.4版本,但请注意,Mosquitto 1.4尚未公布。


2
投票

一个快速的方法来测试你的经纪人是不是造成问题的原因是连接到broker.mqttdashboard.com端口:8000如果不工作,我的下一个猜测是,你刚才安装mosquitto并没有WebSockets的服务器,你需要的,如果你想使用JS直接连接到代理在网上。

另一种,但更快的方式得到了,现在运行的是下载hivemq(试用版支持25个连接),它具有内置的WebSockets一个MQTT经纪人,将在Windows上运行并且将启动并在5分钟运行。


1
投票

哪个版本的Mosquitto您使用的是?

目前的发行版本(1.3.4)本身不支持WebSockets的(下一版本将)

您可以使用类似与mod_websockets提供的WebSocket支持的lighttpd(对于Linux指令链接到从这里:http://test.mosquitto.org/ws.html),也可以从源代码树的头部构筑Mosquitto的新版本

© www.soinside.com 2019 - 2024. All rights reserved.