emqx / qmqtt

MQTT client for Qt

Home Page:https://www.emqx.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The will message doesn't work

kenllf opened this issue · comments

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!

commented

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?

commented

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.

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

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