From f083de5a7d4bbc296e5fa07707a4a179f780f85b Mon Sep 17 00:00:00 2001 From: xengineering Date: Sat, 17 Jan 2026 20:53:07 +0100 Subject: Add automatic reconnecting This allows to use the app after a temporary disconnect between app and MQTT broker. --- lib/data.dart | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/data.dart') diff --git a/lib/data.dart b/lib/data.dart index f5a24e8..38fd564 100644 --- a/lib/data.dart +++ b/lib/data.dart @@ -32,11 +32,14 @@ class AppState with ChangeNotifier { _client.port = brokerPort; _client.keepAlivePeriod = 2; + _client.autoReconnect = true; _client.disconnectOnNoResponsePeriod = 1; _client.logging(on: false); _client.onConnected = _onConnected; _client.onDisconnected = _onDisconnected; + _client.onAutoReconnect = _onAutoReconnect; + _client.onAutoReconnected = _onAutoReconnected; try { await _client.connect(); @@ -59,6 +62,16 @@ class AppState with ChangeNotifier { notifyListeners(); } + void _onAutoReconnect() { + _brokerConnected = false; + notifyListeners(); + } + + void _onAutoReconnected() { + _brokerConnected = true; + notifyListeners(); + } + void _onMessage(List> messages) { for (final MqttReceivedMessage message in messages) { final String topic = message.topic; -- cgit v1.2.3-70-g09d2