From e9307426497a522ae5ad2d4be321b839bc4a8b0f Mon Sep 17 00:00:00 2001 From: Songzex <2402265378@qq.com> Date: Thu, 26 Feb 2026 17:33:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=97TCP=E7=9A=84=E6=8E=A8?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF-=E5=88=9D=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/core/network/tcp/tcp_client.dart | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/core/network/tcp/tcp_client.dart b/lib/core/network/tcp/tcp_client.dart index 4b93cdb6..e24c165b 100644 --- a/lib/core/network/tcp/tcp_client.dart +++ b/lib/core/network/tcp/tcp_client.dart @@ -35,6 +35,13 @@ class TcpClient { debugPrint('收到服务端心跳,自动回复...'); sendHeartbeat(); // 回复 AB AA FF AA AB } + + // 🔥 处理推送消息 + if (isPushMsg(packet)) { + debugPrint('收到推送消息,开始处理...'); + handlePushMessage(packet); // 自定义处理逻辑 + } + _controller.add(packet); } }, @@ -108,5 +115,43 @@ class TcpClient { } +/// 判断是否是推送消息 + bool isPushMsg(RawPacket packet) { + return packet.command == 0x12; + } + /// 处理推送消息 + /// 处理推送消息 + void handlePushMessage(RawPacket packet) { + try { + // 打印详细信息 + debugPrint('收到推送消息:命令码=${packet.command}, 时间=${DateTime.now()}'); + + // 解码 payload 为字符串 + final jsonString = utf8.decode(packet.payload); + debugPrint('推送内容:$jsonString'); + + // 如果是 JSON 格式,进一步解析 + final jsonData = jsonDecode(jsonString) as Map; + if (jsonData.containsKey('event')) { + final event = jsonData['event']; + final deviceId = jsonData['deviceId'] ?? '未知设备'; + final status = jsonData['status'] ?? '未知状态'; + + debugPrint('事件类型:$event, 设备ID:$deviceId, 状态:$status'); + + // 根据事件类型处理逻辑 + if (event == 'device_status_changed') { + // 示例:更新设备状态 + debugPrint('设备状态变更:$deviceId -> $status'); + } + } + } catch (e) { + // 异常处理 + debugPrint('推送消息解析失败:${e.toString()}'); + } + } + + + }