简体   繁体   中英

“Will message” that includes Mosquitto server's time

In MQTT "will messages" can be stored on server by client. 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:

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!

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) ?

No, the LWT message will be published totally unchanged from what is set at the point of creation.

You should be able to find when the client is disconnected from the mosquitto logs.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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