加入 连接状态判断 控制台日志输出

This commit is contained in:
2026-02-05 11:39:27 +08:00
parent f772e16119
commit 9e3a7d5721
7 changed files with 134 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ import 'package:typed_data/src/typed_buffer.dart';
class Mqtt {
final MqttConfig _config;
MqttClient? _client;
bool _connected = false;
//标记是否自动重连
bool reconnect = true;
//缓存自定义主题订阅信息
@@ -26,10 +27,12 @@ class Mqtt {
// _client?.autoReconnect = true;
_client?.onConnected = () {
// print("aa:mqtt服务器连接成功");
_connected = true;
print("mqtt服务器: ${_config.host} 连接成功");
};
_client?.onDisconnected = () {
// print("aa:mqtt服务器连接断开");
_connected = false;
print("mqtt服务器: ${_config.host} 连接断开");
//执行重连操作
if (reconnect) {
@@ -48,6 +51,10 @@ class Mqtt {
_mqtt = server;
}
bool isConnected() {
return this._connected;
}
Future<bool> connect({bool reconnect = false}) async {
if (reconnect) {
//限制重连速率