简体   繁体   中英

Configuration of Mosquitto Server to receive message from javascript

I have a general question about Mosquitto and MQTT. I managed to setup a Server running Ubuntu and Mosquitto 1.4.2 with websockets ( tutorial here ). And I want so send some data from a Javascript Application using the mqttws31.js. Both work fine when subscribing to test.mosquitto.org but when I try to to send the "Hello world" to my own server I get a timeout.

What I tried is this:

ubuntu@instanz6:~$ mosquitto -c /etc/mosquitto/mosquitto.conf
1440670543: mosquitto version 1.4.2 (build date 2015-08-27 11:26:53+0200) starting
1440670543: Config loaded from /etc/mosquitto/mosquitto.conf.
1440670543: Opening websockets listen socket on port 9001.
1440670543: Opening ipv4 listen socket on port 1883.
1440670543: Opening ipv6 listen socket on port 1883.

and then open the html file shown below. I guess that I'm making a general error / missed a concept but I don't know how to continue ...

<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  <script src="js/mqttws31.js" type="text/javascript"></script>
  <script type="text/javascript">

    var wsbroker = "11.111.11.111";
    var wsport = 1883;
    var client = new Paho.MQTT.Client(wsbroker, wsport,
        "myclientid_" + parseInt(Math.random() * 100, 10));
    client.onConnectionLost = function (responseObject) {
      console.log("connection lost: " + responseObject.errorMessage);
    };
    client.onMessageArrived = function (message) {
      console.log(message.destinationName, ' -- ', message.payloadString);
    };
    var options = {
      timeout: 3,
      onSuccess: function () {
        console.log("mqtt connected");
        // Connection succeeded; subscribe to our topic, you can add multile lines of these
        //client.subscribe('/World', {qos: 1});

        //use the below if you want to publish to a topic on connect
        message = new Paho.MQTT.Message("Hello");
        message.destinationName = "/World";
        client.send(message);

      },
      onFailure: function (message) {
        console.log("Connection failed: " + message.errorMessage);
      }
    };
  function init() {
      client.connect(options);
  }
    </script>
  </head>
  <body onload="init();">
  </body>

</html>

You have got the port wrong in your JavaScript, it needs to be port 9001 you have configured for Websockets not 1883

...
var wsbroker = "11.111.11.111";
var wsport = 1883;
var client = new Paho.MQTT.Client(wsbroker, wsport,
    "myclientid_" + parseInt(Math.random() * 100, 10));
...

Should be

...
var wsbroker = "11.111.11.111";
var wsport = 9001;
var client = new Paho.MQTT.Client(wsbroker, wsport,
    "myclientid_" + parseInt(Math.random() * 100, 10));
...

I was even getting the same error Connection failed: AMQJSC0001E Connect timed out.

port is open and through mobile app everything is working fine

Through web app only it was a failure in connection

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