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

Javascript 无法从mqtt.js连接到MOSQUITO服务器,javascript,mqtt,mosquitto,Javascript,Mqtt,Mosquitto,我是wqtt服务器的新手。我正在尝试使用mqtt.jsreffering他们网站上提供的示例连接到mosquitto测试服务器 但我无法连接到服务器。我总是遇到以下错误: WebSocket到“ws://test.mosquitto.org/:8080/mqtt”的连接失败:连接建立中出错:net::ERR\u NAME\u未解析 请帮忙。以下是我的html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h

我是wqtt服务器的新手。我正在尝试使用
mqtt.js
reffering他们网站上提供的示例连接到mosquitto测试服务器

但我无法连接到服务器。我总是遇到以下错误:

WebSocket到“ws://test.mosquitto.org/:8080/mqtt”的连接失败:连接建立中出错:net::ERR\u NAME\u未解析

请帮忙。以下是我的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>

HiveMQ MQTT Websocket演示应用程序
var client=new Messaging.client(“test.mosquitto.org”,8080,“myclientid_u2;”+parseInt(Math.random()*100,10));
//在websocket/mqtt连接因任何原因断开连接时调用
client.onConnectionLost=函数(responseObject){
//根据您的场景,您可以在此处实现重新连接逻辑
警报(“连接丢失:+responseObject.errorMessage”);
};
//每当收到订阅的消息时调用
client.onMessageArrived=函数(消息){
//对收到的推送消息进行处理
$('#messages').append('Topic:'+message.destinationName+'|'+message.payloadString+'');
};
//连接选项
变量选项={
超时时间:3,
//如果已成功建立连接,则调用
onSuccess:函数(){
警报(“已连接”);
},
//如果无法建立连接,则调用
onFailure:功能(消息){
document.write(“连接失败:”+message.errorMessage);
警报(“连接失败:“+message.errorMessage”);
}
};
//创建新的Messaging.Message对象并将其发送到HiveMQ MQTT代理
var publish=功能(有效负载、主题、qos){
//发送消息(也可以将其序列化为JSON或protobuf,或者只使用字符串,没有限制)
var消息=新消息。消息(有效负载);
message.destinationName=主题;
message.qos=qos;
客户端。发送(消息);
}
1.连接
2.订阅
3.发表
(4.断开)

从您的错误消息中,我可以看到两个问题

  • test.mosquitcho.org在8080而不是1883上侦听Websocket连接
  • url中不应包含http://以连接到websocket服务器

  • 此外,错误消息与您在代码示例中包含的详细信息不匹配。

    我尝试了您的解决方案,但没有成功。仍然出现相同问题。编辑时端口号仍然错误。test.mosquitto.org应该是8080,而不是我试过的8083。但是没有解决问题代码仍然是错误的,端口8080不是WebSocket的1883。我希望现在一切正常。:)现在发布的代码(在设置客户端的行中没有单引号)运行良好