简体   繁体   English

包含 Mosquitto 服务器时间的“Will message”

[英]“Will message” that includes Mosquitto server's time

In MQTT "will messages" can be stored on server by client.在 MQTT 中, “遗嘱消息”可以由客户端存储在服务器上。 They are published by the server when the same client disconnects abruptly.当同一个客户端突然断开连接时,它们由服务器发布。 In order to easier debug it would make sense to include date and time inside the "will message" .为了更容易调试,在“遗嘱消息”中包含日期和时间是有意义的。

Currently I am using Paho MQTT library and I define "will message" like this:目前我正在使用 Paho MQTT 库,我像这样定义“将消息”

let _dt = {
    date: () => {
        let _d = new Date();
        let _dd = `${_d.toLocaleDateString("en-US")}`;
        return _dd;
    }, 
    time: () => {
        let _d = new Date();
        let _dd = `${_d.getHours()}:${_d.getMinutes()}:${_d.getSeconds()}.${_d.getMilliseconds()}`;
        return _dd;
    },
}

let _will_message = new Paho.MQTT.Message(`Client "${_client_id}" disconnected abruptly ${_dt.date()} at ${_dt.time()}.`);
_will_message.destinationName = `/will_messages`;
_will_message.retained = true;
_will_message.qos = 2;

But this is clearly not the right way, because "will message" shows the time at which object _will_message is created on the client!但这显然不是正确的方法,因为“will message”显示了在客户端创建 object _will_message的时间!

Is there a way to show the time when Mosquito server realized client disconnected or even better, when disconection actually happened (we would probably need to substract some timeout value) ?有没有办法显示 Mosquito 服务器意识到客户端断开连接甚至更好的时间,实际发生断开连接的时间(我们可能需要减去一些超时值)

No, the LWT message will be published totally unchanged from what is set at the point of creation.不,LWT 消息的发布将与创建时设置的内容完全不同。

You should be able to find when the client is disconnected from the mosquitto logs.您应该能够找到客户端与 mosquitto 日志断开连接的时间。

eg例如

1592604090: Socket error on client mosq-Za0gqtsXkazB5N8Ugt, disconnecting.
1592604238: New connection from 127.0.0.1 on port 1889.
1592604238: New client connected from 127.0.0.1 as mosq-SrUz3EXOh9Bc6huMni (p2, c1, k10).
1592604264: Client mosq-SrUz3EXOh9Bc6huMni has exceeded timeout, disconnecting.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM