From b99c5995b8a2c028960c472e0f706d75c89de5d1 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sun, 22 Mar 2026 21:09:00 +0100
Subject: [PATCH] fix: MQTT connect - disable autoReconnect during trial, retry on all-fail
---
lib/services/mqtt_service.dart | 15 ++++++++++-----
1 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/lib/services/mqtt_service.dart b/lib/services/mqtt_service.dart
index 7ff655c..41dc594 100644
--- a/lib/services/mqtt_service.dart
+++ b/lib/services/mqtt_service.dart
@@ -114,10 +114,14 @@
}
}
- // All hosts failed
- debugPrint('MQTT: all hosts failed');
- _setStatus(ConnectionStatus.disconnected);
- onError?.call('Failed to connect to MQTT broker');
+ // All hosts failed — retry after delay
+ _mqttLog('MQTT: all hosts failed, retrying in 5s');
+ _setStatus(ConnectionStatus.reconnecting);
+ Future.delayed(const Duration(seconds: 5), () {
+ if (!_intentionalClose && _status != ConnectionStatus.connected) {
+ connect();
+ }
+ });
}
/// Returns [localHost, remoteHost] for dual-connect attempts.
@@ -134,7 +138,7 @@
try {
final client = MqttServerClient.withPort(host, clientId, config.port);
client.keepAlivePeriod = 30;
- client.autoReconnect = true;
+ client.autoReconnect = false; // Don't auto-reconnect during trial — enable after success
client.connectTimeoutPeriod = timeout;
client.logging(on: true);
@@ -162,6 +166,7 @@
final result = await client.connect();
_mqttLog('MQTT: connect result=${result?.state}');
if (result?.state == MqttConnectionState.connected) {
+ client.autoReconnect = true; // Now enable auto-reconnect for the live connection
return true;
}
_client = null;
--
Gitblit v1.3.1