简体   繁体   中英

Error While connecting to MQTT Broker using JavaScript MQTT Client With Websockets

I am working with MQTT, when I am trying to connect to mosquitto_sub I am able to connect but When I am trying to connect through JavaScript MQTT Client With Websockets then I am getting an error AMQJSC0001E Connect timed out .

Here is my code :

 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Mosquitto Websockets</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.2/mqttws31.js" type="text/javascript"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js" type="text/javascript"></script> <script type="text/javascript"> var host = "eu.thethings.network"; var username = "sr-ops-rtr-XX"; var password = "ttn-account-v2.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; var topic = "/devices/+/up"; var useTLS = "sr-ops-rtr-01"; cleansession = ""; var port = 10; var mqtt; var reconnectTimeout = 2000; function MQTTconnect() { if (typeof path == "undefined") { path = '/devices/up'; } mqtt = new Paho.MQTT.Client( host, port, path, "web_" + parseInt(Math.random() * 100, 10) ); var options = { timeout: 10, //useSSL: useTLS, //cleanSession: true, onSuccess: onConnect, onFailure: function (message) { console.log(message); $('#status').val("Connection failed: " + message.errorMessage + "Retrying"); setTimeout(MQTTconnect, reconnectTimeout); } }; mqtt.onConnectionLost = onConnectionLost; mqtt.onMessageArrived = onMessageArrived; if (username != null) { options.userName = username; options.password = password; } console.log("Host="+ host + ", port=" + port + ", path=" + path + " username=" + username + " password=" + password); console.log(options); mqtt.connect(options); } function onConnect() { alert("connected"); $('#status').val('Connected to ' + host + ':' + port + path); // Connection succeeded; subscribe to our topic mqtt.subscribe(topic, {qos: 0}); $('#topic').val(topic); } function onConnectionLost(response) { setTimeout(MQTTconnect, reconnectTimeout); $('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting"); }; function onMessageArrived(message) { var topic = message.destinationName; var payload = message.payloadString; $('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>'); }; $(document).ready(function() { MQTTconnect(); }); </script> </head> <body> <h1>Mosquitto Websockets</h1> <div> <div>Subscribed to <input type='text' id='topic' disabled /> Status: <input type='text' id='status' size="80" disabled /></div> <ul id='ws' style="font-family: 'Courier New', Courier, monospace;"></ul> </div> </body> </html> 

What am I doing wrong?

When you load the page a call to MQTTconnect is made ( bottom of the page in code above).

<body>
<h1>Mosquitto Websockets</h1>
<div>
    <div>Subscribed to <input type='text' id='topic' disabled />
    Status: <input type='text' id='status' size="80" disabled /></div>

    <ul id='ws' style="font-family: 'Courier New', Courier, monospace;"></ul>
 </div>
   $(document).ready(function() {
      MQTTconnect();
    });
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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