簡體   English   中英

沒有通過WebSocket從Mosquitto接收有效載荷

[英]Not receiving payloads from mosquitto via websockets

我正在使用VPS,並且正在通過MQTT將數據從Arduino發送到服務器。

Mosquitto通過終端成功打印了有效負載,但是當我嘗試通過網頁實時打印有效負載時,沒有任何反應。

如果我運行,知道我已經在Mosquitto conf中允許使用websockets:

sudo netstat -plnt

我得到:

tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      
13248/mosquitto
tcp        0      0 0.0.0.0:1884            0.0.0.0:*               LISTEN      
20169/mosquitto
tcp6       0      0 :::1883                 :::*                    LISTEN      13248/mosquitto

我要發送的主題名稱是/ pression,而我正在使用的代碼是:

  <script>

    var websocket="myserver.ovh.net";
    var port= 1884;
    var user="username";
    var pass="password";


    client = new Paho.MQTT.Client(websocket, port, "innovation");


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

  var options = {
   useSSL: false,
   userName: user,
   password: pass,
   onSuccess:onConnect,
   onFailure:doFail
}

// connect the client

client.connect(options);


// called when the client connects

 function onConnect() {
// Once a connection has been made, make a subscription and send a 
message.


 document.getElementById("connstatus").innerHTML = "Mqtt Connected";

 console.log("Mqtt Connected");

  client.subscribe("/pression");

    }

    function doFail(e){
    console.log(e);
   }

   // called when the client loses its connection
    function onConnectionLost(responseObject) {

  document.getElementById("connstatus").innerHTML = "Mqtt Not Connected";

     if (responseObject.errorCode !== 0) {
         console.log("onConnectionLost:"+responseObject.errorMessage);
    }
   }

   function onMessageArrived(message) {
   console.log("Pression is :");
   document.getElementById("connstatus").innerHTML = message.payloadString;
   console.log(message.payloadString);

   }


  </script>

當我運行腳本時,它說“ Mqtt Connected”,什么也沒發生。

但是,如果我在終端中運行:

         mosquitto_sub -t '/pression'

我得到壓力值。

如果我將網頁保持打開狀態幾分鍾,則會收到以下消息:

       Mqtt Connected
       test.html:76 onConnectionLost:AMQJS0008I Socket closed.

配置文件:

   # 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 file /var/log/mosquitto/mosquitto.log

   include_dir /etc/mosquitto/conf.d

   #password_file /etc/mosquitto/passwd
   #allow_anonymous false



   listener 1884
   protocol websockets

蚊子日志:

       1557922249: Config loaded from /etc/mosquitto/mosquitto.conf.
       1557922249: Opening websockets listen socket on port 1884.
       1557922254: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
       1557922279: Socket error on client innovation, disconnecting.
       1557922279: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
       1557922318: Socket error on client innovation, disconnecting.
       1557922318: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
       1557922346: Socket error on client innovation, disconnecting.
       1557922346: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
       1557922363: Socket error on client innovation, disconnecting.
       1557922364: New client connected from xx.xx.11.163 as innovation (c1, k60, u'innovation').
       1557922463: Socket error on client innovation, disconnecting.

好的,這里的問題很可能是您在HTML中使用了固定的客戶端ID( innovation )。

您只能將1個客戶端與給定的客戶端ID連接在一起,因此,當一個具有相同ID的新客戶端連接時(例如,重新加載頁面時),代理將斷開最舊的客戶端的連接。

嘗試將連接線更改為如下所示:

var clientID = "innovation_" + new Date().getTime();
client = new Paho.MQTT.Client(websocket, port, clientID);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM