Closed xiaozefeng closed 1 year ago
I will test that to verify, but my initial thought is that by using the default settings you’re making an MQTT v3.1.1 connection. The ability to publish the will message on a clean disconnect is a feature of the MQTT v5 protocol.
Have a look at the mqttrs_chat example, noting that all the examples were updated slightly with the v0.12 release this week. I assume that if you tried it in the last few days, with the paho-mqtt="*"
dependency, you got this version.
I just ran the mqttrs_chat example and it works for me.
As I said, to get the will publication with a clean disconnect, you need a v5 connection. Change your connect options to something like this:
let conn_opts = mqtt::ConnectOptionsBuilder::new_v5()
.keep_alive_interval(Duration::from_secs(20))
.clean_start(false)
.will_message(lwt)
.finalize();
Use new_v5()
to initialize the options for a v5 connection. And then use clean_start()
instead of clean_session()
, for v5.
Got it !
But ConnectOptionsBuilder::new_v5()
have no new_v5() function
, I found ConnectOptionsBuilder::new().mqtt_version(5) , But an error has occurred, the error log
Unable to connect:
PahoDescr(-16, "Wrong MQTT version")
the core code
let conn_opts = mqtt::ConnectOptionsBuilder::new()
.mqtt_version(5)
.keep_alive_interval(Duration::from_secs(20))
// .clean_session(false)
.clean_start(false)
.will_message(lwt)
.finalize();
Am I using the wrong version? This is my Cargo.toml
[dependencies]
paho-mqtt="0.11.1"
v0.12 was released a few days ago.
I would suggest updating to this version as it fixes some bugs and performance issues, but if you want to stay with v0.11, the code you posted should work.
Had the same issue and can confirm that after upgrading dependency to v0.12 and change from
let connect_options = ConnectOptionsBuilder::new()
...
to
let connect_options = ConnectOptionsBuilder::new_v5().clean_start(false)
...
the last will message is send with a .disconnect()
call.
Cargo.toml
main.rs
important code:
Expect: send a will message Actual: not send