【问题标题】:Not receiving payloads from mosquitto via websockets未通过 websockets 从 mosquitto 接收有效负载
【发布时间】:2019-05-15 11:12:17
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: websocket vps mosquitto


    【解决方案1】:

    好的,这里的问题很可能是您在 HTML 中使用了固定的客户端 ID (innovation)。

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

    试着把连接线改成这样:

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

    【讨论】:

    • 我刚刚这样做了,但仍然遇到同样的错误。 1557924290:从 /etc/mosquitto/mosquitto.conf 加载的配置。 1557924290:在端口 1884 上打开 websockets 侦听套接字。 1557924296:从 xx.xx.11.163 连接的新客户端作为创新_1557924296241(c1,k60,u'innovation')。 1557924315:客户端 Innovation_1557924296241 上的套接字错误,正在断开连接。
    • 浏览器和代理之间有防火墙吗?
    • 请问如何查看?
    • 阅读文档,并尝试将 keepAlive 设置为 10 秒而不是默认的 60 秒
    • 感谢 hardillb,我已经尝试了所有方法,但没有任何效果,这可能是 SSL 问题?因为我已经用 cloudMQTT 尝试了我的 js 代码并且它运行良好,但是当我使用我自己的 VPS 时,我遇到了这个问题,唯一的区别是在 cloudMQTT 中我使用了安全的 websocket。
    猜你喜欢
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多