Advertisement
odilonafonso

mqttConnection.js

Nov 17th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    var robuino_operador = "";
  2.    var robuino_local = "jarduino_2c72";
  3.    var host = 'broker.mqttdashboard.com';   // hostname or IP address
  4.    var path = "/ws";
  5.    var port = 8000;
  6.  
  7.    function MQTTconnect() {
  8.       if (mqttConnected) {
  9.          mqttConnected = false;
  10.          console.info("already connected !!");
  11.          //~ mqtt.disconnect();
  12.          return;
  13.       }
  14.       if (typeof path == "undefined") {
  15.          path = '/ws';
  16.       }
  17.       mqtt = new Paho.MQTT.Client(
  18.          host,
  19.          port,
  20.          path,
  21.          robuino_local + parseInt(Math.random() * 100, 10)
  22.       );
  23.       var options = {
  24.          timeout: 3,
  25.          useSSL: useTLS,
  26.          cleanSession: cleansession,
  27.          onSuccess: onConnect,
  28.          onFailure: function (message) {
  29.              $('#status').val("Connection failed: " + message.errorMessage + "Retrying");
  30.              setTimeout(MQTTconnect, reconnectTimeout);
  31.          }
  32.       };
  33.  
  34.       mqtt.onConnectionLost = onConnectionLost;
  35.       mqtt.onMessageArrived = onMessageArrived;
  36.  
  37.       if (username != null) {
  38.          options.userName = username;
  39.          options.password = password;
  40.       }
  41.       var logx = "Host="+ host + ", port=" + port + ", path=" + path + " TLS = " + useTLS + " username=" + username + " password=" + password;
  42.       console.log(logx);
  43.       mqtt.connect(options);
  44.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement