Cannot (reconnect) to mosquitto after broker restarted [message #1849822] |
Thu, 03 February 2022 10:15  |
Eclipse User |
|
|
|
Hello and thanks for reading,
I have trouble re-connecting to mosquitto after it has been restrated.
At first, my program is registered to mosquitto as expected. However, after the broker is restarted (creating a new instance of the broker; I have no control over that) I must re-establish connection with it but the call to connect() below hangs:
mosquitto_lib_cleanup();
reinitialise(("mosq/" + m_ClientId + "/" + m_ServiceName + "//").c_str(), true);
if (mosqpp::lib_init() != MOSQ_ERR_SUCCESS)
{
throw std::runtime_error("CMQTTCommunicator: can't intialize library.");
}
mosqpp::mosquittopp::int_option(MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
// Last will and testament
const std::string WillMessage = Json(Json::object{{ "service", m_ServiceName }, {"status", "Disconnected"}}).dump();
will_set((m_FullServiceIdentifier + "status").c_str(), WillMessage.length(), WillMessage.c_str(), static_cast<int>(EQualityOfService::DeliverAtLeastOnce));
if (connect(m_MQTTBrokerAddress.GetIPAddress().ToString().c_str(), m_MQTTBrokerAddress.GetPort(), KeepAliveSec) != MOSQ_ERR_SUCCESS)
{
throw CApplicationException("CMQTTCommunicator: could not connect to mosquitto broker");
}
I do not think I can use reconnect() here at the broker is restarted.
Do you see why this is the case? What am I missing?
Thanks in advance!
[Updated on: Fri, 04 February 2022 03:04] by Moderator
|
|
|
|
Re: Cannot (reconnect) to mosquitto after broker restarted [message #1849844 is a reply to message #1849827] |
Fri, 04 February 2022 04:29  |
Eclipse User |
|
|
|
Hello,
Thanks for your reply.
Unfortunately this does not change the situation: occasionally reconnection fails (it is not easy for me to debug this, but from the behavior I observe it is clear connection is not established).
To be clear: the method you suggested should always work, even if a new instance of the broker is spawned?
[Updated on: Fri, 04 February 2022 04:30] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.06227 seconds