Matthias Nott
2026-03-22 b99c5995b8a2c028960c472e0f706d75c89de5d1
fix: MQTT connect - disable autoReconnect during trial, retry on all-fail
1 files modified
changed files
lib/services/mqtt_service.dart patch | view | blame | history
lib/services/mqtt_service.dart
....@@ -114,10 +114,14 @@
114114 }
115115 }
116116
117
- // All hosts failed
118
- debugPrint('MQTT: all hosts failed');
119
- _setStatus(ConnectionStatus.disconnected);
120
- onError?.call('Failed to connect to MQTT broker');
117
+ // All hosts failed — retry after delay
118
+ _mqttLog('MQTT: all hosts failed, retrying in 5s');
119
+ _setStatus(ConnectionStatus.reconnecting);
120
+ Future.delayed(const Duration(seconds: 5), () {
121
+ if (!_intentionalClose && _status != ConnectionStatus.connected) {
122
+ connect();
123
+ }
124
+ });
121125 }
122126
123127 /// Returns [localHost, remoteHost] for dual-connect attempts.
....@@ -134,7 +138,7 @@
134138 try {
135139 final client = MqttServerClient.withPort(host, clientId, config.port);
136140 client.keepAlivePeriod = 30;
137
- client.autoReconnect = true;
141
+ client.autoReconnect = false; // Don't auto-reconnect during trial — enable after success
138142 client.connectTimeoutPeriod = timeout;
139143 client.logging(on: true);
140144
....@@ -162,6 +166,7 @@
162166 final result = await client.connect();
163167 _mqttLog('MQTT: connect result=${result?.state}');
164168 if (result?.state == MqttConnectionState.connected) {
169
+ client.autoReconnect = true; // Now enable auto-reconnect for the live connection
165170 return true;
166171 }
167172 _client = null;