Skip to content

Commit 10dc070

Browse files
Check connection and flush queue of buffered messages
1 parent d1c823d commit 10dc070

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Mqtt/MqttProcessor.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "Logger.h"
2121

2222
#include <string>
23+
#include <chrono>
24+
25+
using namespace std::chrono_literals;
2326

2427
namespace {
2528

@@ -131,11 +134,13 @@ namespace sua {
131134
{
132135
Logger::info("MQTT broker address: '{}:{}'", _config.brokerHost, _config.brokerPort);
133136

134-
mqtt::connect_options options;
137+
static mqtt::connect_options options;
135138
options.set_clean_session(false);
136139
options.set_servers(std::make_shared<mqtt::string_collection>(
137140
fmt::format("{}:{}", _config.brokerHost, _config.brokerPort)
138141
));
142+
options.set_automatic_reconnect(true);
143+
options.set_connect_timeout(5000ms);
139144

140145
static MqttCallback callback(_client, options, _context);
141146
_client.set_callback(callback);
@@ -176,6 +181,10 @@ namespace sua {
176181
message->set_qos(QUALITY);
177182
message->set_retained(retained);
178183
_client.publish(message);
184+
185+
if(!_client.is_connected() || !_client.get_pending_delivery_tokens().empty()) {
186+
_client.reconnect();
187+
}
179188
}
180189

181190
} // namespace sua

0 commit comments

Comments
 (0)