支持现场服务器地址配置
This commit is contained in:
@@ -23,6 +23,7 @@ class MqttClientImpl implements MqttClient {
|
||||
bool _isConnected = false;
|
||||
bool _isConnecting = false;
|
||||
bool _manualDisconnect = false;
|
||||
int _connectionGeneration = 0;
|
||||
|
||||
MqttClientImpl({this.clientName = 'MqttClient'});
|
||||
|
||||
@@ -39,11 +40,12 @@ class MqttClientImpl implements MqttClient {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_isConnected) {
|
||||
debugPrint('[MqttClient] already connected, disconnect first');
|
||||
await disconnect();
|
||||
}
|
||||
|
||||
final generation = ++_connectionGeneration;
|
||||
final previousClient = _client;
|
||||
final previousUpdates = _updatesSubscription;
|
||||
_client = null;
|
||||
_updatesSubscription = null;
|
||||
_isConnected = false;
|
||||
_manualDisconnect = false;
|
||||
_currentConfig = config;
|
||||
_reconnectTimer?.cancel();
|
||||
@@ -51,33 +53,45 @@ class MqttClientImpl implements MqttClient {
|
||||
_isConnecting = true;
|
||||
|
||||
try {
|
||||
await _updatesSubscription?.cancel();
|
||||
_updatesSubscription = null;
|
||||
_client = _createClient(config);
|
||||
_configureClient(_client!, config);
|
||||
await previousUpdates?.cancel();
|
||||
previousClient?.disconnect();
|
||||
if (generation != _connectionGeneration) return;
|
||||
|
||||
debugPrint('${LogTime.now()} 🔌 [$clientName] 正在连接 ${config.connectionAddress}:${config.port} (clientId=${config.clientId})');
|
||||
await _client!.connect(config.username, config.password);
|
||||
final client = _createClient(config);
|
||||
_client = client;
|
||||
_configureClient(client, config);
|
||||
|
||||
if (_client!.connectionStatus?.state ==
|
||||
debugPrint(
|
||||
'${LogTime.now()} 🔌 [$clientName] 正在连接 ${config.connectionAddress}:${config.port} (clientId=${config.clientId})',
|
||||
);
|
||||
await client.connect(config.username, config.password);
|
||||
if (generation != _connectionGeneration) {
|
||||
client.disconnect();
|
||||
return;
|
||||
}
|
||||
|
||||
if (client.connectionStatus?.state ==
|
||||
mqtt.MqttConnectionState.connected) {
|
||||
_isConnected = true;
|
||||
debugPrint('${LogTime.now()} ✅ [$clientName] 连接成功 ${config.connectionAddress}:${config.port}');
|
||||
debugPrint(
|
||||
'${LogTime.now()} ✅ [$clientName] 连接成功 ${config.connectionAddress}:${config.port}',
|
||||
);
|
||||
_listenToMessages();
|
||||
_resubscribeAll();
|
||||
} else {
|
||||
throw Exception(
|
||||
'MQTT connect failed: ${_client!.connectionStatus?.returnCode}',
|
||||
'MQTT connect failed: ${client.connectionStatus?.returnCode}',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
if (generation != _connectionGeneration) return;
|
||||
debugPrint('${LogTime.now()} ❌ [$clientName] 连接失败: $e');
|
||||
_isConnected = false;
|
||||
_isConnecting = false;
|
||||
_scheduleReconnect();
|
||||
rethrow;
|
||||
} finally {
|
||||
_isConnecting = false;
|
||||
if (generation == _connectionGeneration) _isConnecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,9 +127,15 @@ class MqttClientImpl implements MqttClient {
|
||||
client.keepAlivePeriod = config.keepAlivePeriod;
|
||||
client.autoReconnect = false;
|
||||
client.resubscribeOnAutoReconnect = false;
|
||||
client.onDisconnected = _onDisconnected;
|
||||
client.onConnected = _onConnected;
|
||||
client.onSubscribed = _onSubscribed;
|
||||
client.onDisconnected = () {
|
||||
if (identical(_client, client)) _onDisconnected();
|
||||
};
|
||||
client.onConnected = () {
|
||||
if (identical(_client, client)) _onConnected();
|
||||
};
|
||||
client.onSubscribed = (topic) {
|
||||
if (identical(_client, client)) _onSubscribed(topic);
|
||||
};
|
||||
|
||||
var connMessage = mqtt.MqttConnectMessage()
|
||||
.withClientIdentifier(client.clientIdentifier)
|
||||
@@ -131,16 +151,19 @@ class MqttClientImpl implements MqttClient {
|
||||
@override
|
||||
Future<void> disconnect() async {
|
||||
debugPrint('[MqttClient] disconnect');
|
||||
_connectionGeneration++;
|
||||
final client = _client;
|
||||
final updates = _updatesSubscription;
|
||||
_manualDisconnect = true;
|
||||
_reconnectTimer?.cancel();
|
||||
_reconnectTimer = null;
|
||||
await _updatesSubscription?.cancel();
|
||||
_updatesSubscription = null;
|
||||
_client?.disconnect();
|
||||
_client = null;
|
||||
_isConnected = false;
|
||||
_isConnecting = false;
|
||||
_currentConfig = null;
|
||||
await updates?.cancel();
|
||||
client?.disconnect();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -196,8 +219,10 @@ class MqttClientImpl implements MqttClient {
|
||||
}
|
||||
|
||||
void _listenToMessages() {
|
||||
_updatesSubscription = _client!.updates!.listen(
|
||||
final client = _client!;
|
||||
_updatesSubscription = client.updates!.listen(
|
||||
(List<mqtt.MqttReceivedMessage<mqtt.MqttMessage>> messages) {
|
||||
if (!identical(_client, client)) return;
|
||||
for (final msg in messages) {
|
||||
final topic = msg.topic;
|
||||
final payload = mqtt.MqttPublishPayload.bytesToStringAsString(
|
||||
@@ -209,6 +234,7 @@ class MqttClientImpl implements MqttClient {
|
||||
}
|
||||
},
|
||||
onError: (Object error) {
|
||||
if (!identical(_client, client)) return;
|
||||
debugPrint('[MqttClient] updates stream error: $error');
|
||||
_isConnected = false;
|
||||
_scheduleReconnect();
|
||||
|
||||
Reference in New Issue
Block a user