简体   繁体   English

无法从mqtt.js连接到Mosquitto服务器

[英]Not able to connect to Mosquitto Server from mqtt.js

I am new to wqtt server. 我是wqtt服务器的新手。 I am trying to connect to mosquitto test server using mqtt.js reffering an example provided on their website. 我正在尝试使用mqtt.js并根据其网站上提供的示例连接到mosquitto测试服务器。

But i am not able to connect to the server. 但是我无法连接到服务器。 I always get following error: 我总是收到以下错误:

WebSocket connection to 'ws://test.mosquitto.org/:8080/mqtt' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED . WebSocket connection to 'ws://test.mosquitto.org/:8080/mqtt' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED

Please help. 请帮忙。 Below is my html: 以下是我的html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
    <script src="http://www.hivemq.com/demos/websocket-client/js/mqttws31.js" type="text/javascript"></script>
    <title>HiveMQ MQTT Websocket Demo App</title>
    <script type="text/javascript">
      var client = new Messaging.Client("test.mosquitto.org", 8080, "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);
 }
    </script>
</head>
<body>
    <button onclick="client.connect(options);">1. Connect</button>
    <button onclick="client.subscribe('testtopic/#', {qos: 2}); alert('Subscribed');">2. Subscribe</button>
    <button onclick="publish('Hello Foo !','testtopic/bar',2);">3. Publish</button>
    <button onclick="client.disconnect();">(4. Disconnect)</button>
    <div id="messages"></div>
</body>

From your error message I can see 2 issues. 从您的错误消息中,我可以看到2个问题。

  1. test.mosquitto.org listens for Websocket connections on 8080 not 1883 test.mosquitto.org在8080而非1883上侦听Websocket连接
  2. There should be no http:// in the url to connect to a websocket server 网址中不应包含http://以连接到websocket服务器

Also the error message does not match the details you have included in the code sample. 此外,错误消息与代码示例中包含的详细信息不匹配。

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

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