| .. | .. |
|---|
| 114 | 114 | } |
|---|
| 115 | 115 | } |
|---|
| 116 | 116 | |
|---|
| 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 | + }); |
|---|
| 121 | 125 | } |
|---|
| 122 | 126 | |
|---|
| 123 | 127 | /// Returns [localHost, remoteHost] for dual-connect attempts. |
|---|
| .. | .. |
|---|
| 134 | 138 | try { |
|---|
| 135 | 139 | final client = MqttServerClient.withPort(host, clientId, config.port); |
|---|
| 136 | 140 | client.keepAlivePeriod = 30; |
|---|
| 137 | | - client.autoReconnect = true; |
|---|
| 141 | + client.autoReconnect = false; // Don't auto-reconnect during trial — enable after success |
|---|
| 138 | 142 | client.connectTimeoutPeriod = timeout; |
|---|
| 139 | 143 | client.logging(on: true); |
|---|
| 140 | 144 | |
|---|
| .. | .. |
|---|
| 162 | 166 | final result = await client.connect(); |
|---|
| 163 | 167 | _mqttLog('MQTT: connect result=${result?.state}'); |
|---|
| 164 | 168 | if (result?.state == MqttConnectionState.connected) { |
|---|
| 169 | + client.autoReconnect = true; // Now enable auto-reconnect for the live connection |
|---|
| 165 | 170 | return true; |
|---|
| 166 | 171 | } |
|---|
| 167 | 172 | _client = null; |
|---|