emqx / qmqtt

MQTT client for Qt
https://www.emqx.com
Other
698 stars 337 forks source link

The will message doesn't work #213

Closed kenllf closed 4 years ago

kenllf commented 4 years ago

I use the Qt official library : https://codereview.qt-project.org/admin/repos/qt/qtmqtt And i use mosquitto as server. I set the will message with the method:

QMqttClient::setWillMessage()

QMqttClient::setWillTopic()

When i kill one of the mqtt-client, the other client didn't receive any message.

It's ok when i send other message with #QMqttClient::publish(). And the "will message" function works well on the program i download from the internet. Why it didn't work on my program? Could somebody help me? Thank you!

ejvr commented 4 years ago

Tried this with mosquitto on debian linux. There are 2 things you want to consider:

  1. You should call setWillMessage and setWillTopic before connecting to the server, because the will data will be sent to the server when connecting. Setting it later will have no effect until you reconnect.
  2. A clean disconnect (eg. calling Client::close) from the server will not cause the will message to be sent (tested with mosquiito only). So it depends on how you 'kill' the client application. I started the application from the command line and killed it using ctrl+C. This worked.

On 05-Nov-20 09:49:21, kenllf notifications@github.com wrote:

I use the Qt official library : https://codereview.qt-project.org/admin/repos/qt/qtmqtt And i use mosquitto as server. I set the will message with the method:

QMqttClient::setWillMessage()

QMqttClient::setWillTopic()

When i kill one of the mqtt-client, the other client didn't receive any message.

It's ok when i send other message with #QMqttClient::publish(). And the "will message" function works well on the program i download from the internet. Why it didn't work on my program? Could somebody help me? Thank you!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

kenllf commented 4 years ago

Thanks for your reply. I have called setWillMessage and setWillTopic before connecting to the server. I captured mqtt packets with Wireshark and found that there was still disconnect packet sent from client when I closed the program directly, without calling #disconnectFromHost(). That's why there was no "will message" sent from the server. But I am still confused why there was still disconnect packet sent from client when I close the program directly, without calling #disconnectFromHost(). Is that not a right way to "kill" a client?

ejvr commented 4 years ago

Hmm, I just tried this with the example project with the repo with setWillMessage and setWillTopic added. The app run send a number of messages to the broker and does a clean shutdown after that (that is: it call Client:::disconnectFromHost and quits the application after the Client::disconnected has been fired). In that case the broker does not publish a will message.

However, if you quit the application without calling Client:::disconnectFromHost, the broker will publish the will message.

I guess your scenario looks like the latter. What you could do to find out what happens is run your application in a debugger and put a break point in the function ClientPrivate::sendDisconnect in qmqtt_client_p.cpp. After hitting the break point the call stack should tell you how you got there.

kenllf commented 4 years ago

Thank you very much! I think the client had somehow called disconnectFromHost. I will try your suggestion.

mwallnoefer commented 4 years ago

Closing this, please feel free to re-open in case of further clarifications.