Mosquitto MQTT有效载荷是在终端上接收的,还是在Websocket上没有?

时间:2019-05-07 15:19:57

标签: javascript websocket mqtt mosquitto

使用外部TCP / IP设备,我可以通过终端从设备接收MQTT负载,但是当我在烧瓶应用程序中使用JavaScript实施时,我可以连接并订阅该主题,但是没有数据通过

我已经尝试通过在线MQTT示例http://www.lazyengineers.com/mqtt-websocket/and使用相同的端口和IP地址进行订阅,但存在相同的问题。我需要在mosquitto.conf中添加一些内容才能使其在Websocket上正常工作吗?

我的mosquitto.conf文件如下所示

 # Place your local configuration in /etc/mosquitto/conf.d/
 #
 # A full description of the configuration file is at
 # /usr/share/doc/mosquitto/examples/mosquitto.conf.example

 pid_file /var/run/mosquitto.pid

 persistence true
 persistence_location /var/lib/mosquitto/

 log_dest topic
 log_type error
 log_type warning
 log_type notice
 log_dest file /var/log/mosquitto/mosquitto.log

 include_dir /etc/mosquitto/conf.d

 listener 8083
 protocol websockets

在我的JavaScript中,我使用PahoClient并使用host:10.0.0.15端口8083和随机CLientID进行连接。我订阅了Advantech / 00D0C9FA9984 / data主题。与代理和主题的连接正常,但是在订阅主题之后,没有数据通过。我的代码如下:

 <div class="wrapper">
 <h1>mqtt-demo</h1>
 <form id="connection-information-form">
    <b>Hostname or IP Address:</b> 
    <input id="host" type="text" name="host" value="10.0.0.15"> 
    <br>

    <b>Port:</b>
    <input id="port" type="text" name="port" value="8083"><br>
    <b>Topic:</b>
    <input id="topic" type="text" name="topic" 
    value="Advantech/00D0C9FA9984/data"><br><br>

    <input type="button" onclick="startConnect()" value="Connect">
    <input type="button" onclick="startDisconnect()" 
   value="Disconnect">
 </form>
 <div id="messages"></div>
 </div>   


  <script>
 // Called after form input is processed
 function startConnect() {
   // Generate a random client ID
   clientID = "clientID-" + parseInt(Math.random() * 100);

   // Fetch the hostname/IP address and port number from the form
   host = document.getElementById("host").value;
   port = document.getElementById("port").value;

   // Print output for the user in the messages div
   document.getElementById("messages").innerHTML += 
    '<span>Connecting to: ' + host + ' on port: ' + port + 
    '</span><br/>';
   document.getElementById("messages").innerHTML += '<span>Using the following client value: ' + clientID + '</span><br/>';

   // Initialize new Paho client connection
   client = new Paho.MQTT.Client(host, Number(port), clientID);

   // Set callback handlers
   client.onConnectionLost = onConnectionLost;
   client.onMessageArrived = onMessageArrived;

   // Connect the client, if successful, call onConnect function
   client.connect({ 
     onSuccess: onConnect,
   });
 }

 // Called when the client connects
 function onConnect() {
   // Fetch the MQTT topic from the form
   topic = document.getElementById("topic").value;

   // Print output for the user in the messages div
   document.getElementById("messages").innerHTML += '<span>Subscribing to: ' + topic + '</span><br/>';

   // Subscribe to the requested topic
   client.subscribe(topic);
 }

 // Called when the client loses its connection
 function onConnectionLost(responseObject) {
   document.getElementById("messages").innerHTML += '<span>ERROR: Connection lost</span><br/>';
   if (responseObject.errorCode !== 0) {
     document.getElementById("messages").innerHTML += '<span>ERROR: ' + + responseObject.errorMessage + '</span><br/>';
   }
 }  

 // Called when a message arrives
 function onMessageArrived(message) {
   console.log("onMessageArrived: " + message.payloadString);
   document.getElementById("messages").innerHTML += '<span>Topic: ' + 
   message.destinationName + '  | ' + message.payloadString + '</span <br/>';
 }

 // Called when the disconnection button is pressed
 function startDisconnect() {
   client.disconnect();
   document.getElementById("messages").innerHTML += '<span>Disconnected</span><br/>';
 }
  </script>

当我在终端中运行以下命令时:

mosquitto_sub -h 10.0.0.15 -t Advantech/00D0C9FA9984/data

我看到以下数据:

{"s":1,"t":0,"q":192,"c":1,"ai1":4.000,"ai_st1":1,"ai2":-0.000,"ai_st2":1,"ai3":-0.000,"ai_st3":1,"ai4":-0.000,"ai_st4":1,"ai5":-0.000,"ai_st5":1,"ai6":-0.000,"ai_st6":1,"ai7":-0.000,"ai_st7":1,"ai8":-0.000,"ai_st8":1,"do1":false,"do_st1":1,"do2":false,"do_st2":1}

我希望看到我的<div id="messages"></div>

中的有效载荷正在更新

0 个答案:

没有答案