首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未通过websockets接收来自蚊子的有效负载

未通过websockets接收来自蚊子的有效负载
EN

Stack Overflow用户
提问于 2019-05-15 19:12:17
回答 1查看 79关注 0票数 0

我正在使用VPS,并通过MQTT将数据从Arduino发送到服务器。

Mosquitto成功地通过终端打印有效载荷,但当我尝试通过网页实时打印它时,没有任何反应。

知道我已经在Mosquitto conf中允许websockets,如果我运行:

代码语言:javascript
复制
sudo netstat -plnt

我得到了:

代码语言:javascript
复制
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

我发送的主题name : /pression和我使用的代码是:

代码语言:javascript
复制
  <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已连接“,而不是什么都没有发生。

但是,如果我在终端中运行:

代码语言:javascript
复制
         mosquitto_sub -t '/pression'

我得到了压力值。

如果我让网页打开几分钟,我会得到这样的信息:

代码语言:javascript
复制
       Mqtt Connected
       test.html:76 onConnectionLost:AMQJS0008I Socket closed.

配置文件:

代码语言:javascript
复制
   # 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

蚊子记录:

代码语言:javascript
复制
       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.
EN

回答 1

Stack Overflow用户

发布于 2019-05-15 20:39:27

好的,这里的问题很可能是你在超文本标记语言中使用了一个固定的客户id (innovation)。

您只能有一个客户端与给定的客户端id连接,因此当具有相同id的新客户端连接时(例如,当您重新加载页面时),代理将断开最旧的客户端。

尝试将连接线更改为如下所示:

代码语言:javascript
复制
var clientID = "innovation_" + new Date().getTime();
client = new Paho.MQTT.Client(websocket, port, clientID);
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56147991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档