nodejs mqtt 在收到第一条消息后立即向代理发送断开连接,应该保持订阅状态

nodejs mqtt immediately sends disconnect to broker after receiving first messages, should stay subscribed instead

背景

我在 Raspberry Pi 上设置了一个 mosquitto mqtt 代理。此外,我有一个物联网设备(Shelly Flood),可以将 mqtt 数据包发送到代理,例如。当我检测到漏水时。

我想要一个 nodejs 脚本来订阅某个主题,并在代理收到有关该主题的消息后立即执行一些操作。

行为

我可以启动 nodejs 脚本,并且我完美地接收了 IoT 设备发送的关于该主题的最后一条消息。但是,我没有收到关于这些主题的任何更新,尽管它们已发送。根据 mosquitto 日志,明显的原因是节点 js 或我的脚本向代理发送了 DISCONNECT 消息。

nodejs 脚本

var mqtt = require('/usr/lib/node_modules/mqtt')

const mqttBroker = 'mqtt://192.168.11.19';
const clientId = 'nodejs-mqtt@Raspberry2';
const myTopics = ['#'];

var client  = mqtt.connect(mqttBroker, {clientId:clientId, keepalive:5000, reconnectPeriod: 1000 * 1})

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(topic.toString())
  console.log(message.toString())
  client.end()
})

client.on('connect', function () {
  client.subscribe(myTopics, function (err) {
    if (!err) {
       console.log ('subscribed to ' + mqttBroker + ' for topics ' + myTopics.join(', '));
       // client.publish('/nodejs', 'Hello mqtt')
    }
  })


  console.log('connected to ' + mqttBroker + ' as ' + clientId);

  // start sending
  var i = 0;
  setInterval(
    function(){
        var message = i.toString();
        console.log("sending ", message)
        client.publish("test", message, {qos: 1}, function(){
            console.log("sent ", message)
        });
        i += 1;
     },
   3000);
})

client.on("error", function(error) {
    console.log("ERROR: ", error);
});

client.on('offline', function() {
    console.log("offline");
});

client.on('reconnect', function() {
    console.log("reconnect");
});
# Script output #
connected to mqtt://192.168.11.19 as nodejs-mqtt@Raspberry2
subscribed to mqtt://192.168.11.19 for topics #
shellies/shellyflood-C8A9D2/online
true
shellies/shellyflood-C8A9D2/sensor/temperature
25.12
shellies/shellyflood-C8A9D2/sensor/flood
false
shellies/shellyflood-C8A9D2/sensor/battery
69
shellies/shellyflood-C8A9D2/sensor/error
0
shellies/shellyflood-C8A9D2/sensor/act_reasons
["sensor"]
sending  0
sent  0
sending  1
sent  1
sending  2
sent  2

蚊子日志

1624712368: New connection from 192.168.11.19 on port 1883.
1624712368: New client connected from 192.168.11.19 as nodejs-mqtt@Raspberry2 (c1, k5000).
1624712368: No will message specified.
1624712368: Sending CONNACK to nodejs-mqtt@Raspberry2 (0, 0)
1624712368: Received SUBSCRIBE from nodejs-mqtt@Raspberry2
1624712368:     # (QoS 0)
1624712368: nodejs-mqtt@Raspberry2 0 #
1624712368: Sending SUBACK to nodejs-mqtt@Raspberry2
1624712371: Received PUBLISH from nodejs-mqtt@Raspberry2 (d0, q1, r0, m12577, 'test', ... (1 bytes))
1624712371: Sending PUBACK to nodejs-mqtt@Raspberry2 (Mid: 12577)
1624712371: Sending PUBLISH to mosqsub|23437-raspberry (d0, q0, r0, m0, 'test', ... (1 bytes))
1624712371: Sending PUBLISH to nodejs-mqtt@Raspberry2 (d0, q0, r0, m0, 'test', ... (1 bytes))
1624712371: Received DISCONNECT from nodejs-mqtt@Raspberry2
1624712371: Client nodejs-mqtt@Raspberry2 disconnected.
1624712404: Received PINGREQ from mosqsub|23437-raspberry
1624712404: Sending PINGRESP to mosqsub|23437-raspberry

问题

您是否知道为什么我的客户向 mqtt 代理发送 DISCONNECT,或者 - 面向解决方案 - 我需要改进什么才能让我的客户获得属于订阅主题的消息 'forever'(直到脚本被手动停止)。

非常感谢任何帮助/提示!

您将在 message 处理程序中结束客户端连接:

client.on('message', function (topic, message) {
  console.log(topic.toString())
  console.log(message.toString())
  
  client.end()
  // ^^^^^^
})

删除 client.end() 调用。