From a8037d599d10b3a2fd7fb76b1d7baa3dde12e2c7 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Mon, 11 Sep 2023 17:31:07 +0200 Subject: [PATCH] Fix keepalive timeout being uninitialized in client and allow setting it --- client/main.cpp | 9 +++++++++ libnymea-mqtt/mqttclient_p.h | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/client/main.cpp b/client/main.cpp index 58d412f..36dfe9b 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -48,6 +48,7 @@ int main(int argc, char *argv[]) { {{"payload", "l"}, "Publish payload (requires -p).", "payload"}, {{"retain", "r"}, "Retain flag for publishes (default: no retaining)."}, {{"qos", "q"}, "QoS (default: 1).", "QoS", "1"}, + {{"keepalive", "k"}, "Keep alive timeout in seconds (default: 60)", "keepalive", "60"}, {{"ssl", "S"}, "Use SSL for TCP connections (default: off) (Websocket connections will determine the use of SSL using the url scheme)."}, {{"accept-self-signed-certificate", "A"}, "Ignore self signed certificate errors"}, {{"verbose", "v"}, "Be more verbose"} @@ -73,7 +74,15 @@ int main(int argc, char *argv[]) { bool retain = parser.isSet("retain"); + int keepAlive = parser.value("keepalive").toInt(&ok); + if (!ok || keepAlive < 0 || keepAlive > 65535) { + qCritical() << "Invalid keep alive option. Must be a number from 0 to 65535."; + exit(EXIT_FAILURE); + } + MqttClient client(parser.value("clientid"), app); + client.setKeepAlive(keepAlive); + QObject::connect(&client, &MqttClient::error, app, [&client, app](QAbstractSocket::SocketError socketError){ qCritical() << "Connection error:" << socketError; client.setAutoReconnect(false); diff --git a/libnymea-mqtt/mqttclient_p.h b/libnymea-mqtt/mqttclient_p.h index 814d266..7ee0bf1 100644 --- a/libnymea-mqtt/mqttclient_p.h +++ b/libnymea-mqtt/mqttclient_p.h @@ -77,7 +77,7 @@ public: quint16 maxReconnectTimeout = 36000; QString clientId; - quint16 keepAlive; + quint16 keepAlive = 0; QTimer keepAliveTimer; QString willTopic; QByteArray willMessage;