From 45b0d8ccb950eba4233b6622e0438f46c147490e Mon Sep 17 00:00:00 2001 From: wyf <494641114@qq.com> Date: Fri, 6 Jun 2025 10:54:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0websocket=E9=87=8D=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pages/device/instant_body_page.dart | 46 +++++++++++++++++++++---- lib/pages/main_bottom/mine_page.dart | 2 +- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/lib/pages/device/instant_body_page.dart b/lib/pages/device/instant_body_page.dart index 44af19a..7c56085 100644 --- a/lib/pages/device/instant_body_page.dart +++ b/lib/pages/device/instant_body_page.dart @@ -30,7 +30,8 @@ class InstantBodyPage extends StatefulWidget { State createState() => _InstantBodyPageState(); } -class _InstantBodyPageState extends State { +class _InstantBodyPageState extends State + with WidgetsBindingObserver { GlobalController globalController = Get.find(); UserInfoController userInfoController = Get.find(); BlueteethBindController blueteethBindController = Get.find(); @@ -57,27 +58,53 @@ class _InstantBodyPageState extends State { @override void initState() { + WidgetsBinding.instance.addObserver(this); // 添加生命周期观察者 + // edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage( + // path: "/vsbs/web/rt/marttress", + // type: 1, + // data: {"mac": widget.personInfo['mac']}))); + // _startOnlineTimer(); // 初始化时启动定时器 + _initWebSocket(); + super.initState(); + } + + void _initWebSocket() { + // 发送WebSocket请求 edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage( path: "/vsbs/web/rt/marttress", type: 1, data: {"mac": widget.personInfo['mac']}))); - _startOnlineTimer(); // 初始化时启动定时器 - super.initState(); + _startOnlineTimer(); } + //y @override void dispose() { - _onlineTimer?.cancel(); // 取消定时器,防止内存泄漏 - edm.EasyDartModule.websocket.sendData( - jsonEncode(WebSocketMessage(path: "/vsbs/web/rt/marttress", type: 2))); + WidgetsBinding.instance.removeObserver(this); // 移除生命周期观察者 + _onlineTimer?.cancel(); + _closeWebSocket(); super.dispose(); } + // 监听应用生命周期变化 + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + if (state == AppLifecycleState.resumed) { + // 应用回到前台时重新连接WebSocket + edm.EasyDartModule.logger.info("app切回页面,重连websocket"); + _initWebSocket(); + } else if (state == AppLifecycleState.paused) { + // 应用进入后台时关闭WebSocket + _closeWebSocket(); + } + } + void _startOnlineTimer() { _onlineTimer?.cancel(); // 取消之前的定时器 _onlineTimer = Timer.periodic(Duration(seconds: 30), (timer) { if (mounted) { setState(() { + edm.EasyDartModule.logger.info("30 秒内没有接收到数据,设置为离线"); onlineState = "离线".tr; // 30 秒内没有接收到数据,设置为离线 inBed = "未知数据".tr; bodyMotion = -1; @@ -977,6 +1004,13 @@ class _InstantBodyPageState extends State { ), ); } + + void _closeWebSocket() { + // 取消WebSocket订阅 + edm.EasyDartModule.websocket.sendData( + jsonEncode(WebSocketMessage(path: "/vsbs/web/rt/marttress", type: 2))); + _onlineTimer?.cancel(); + } } class SpeedControlledGif extends StatefulWidget { diff --git a/lib/pages/main_bottom/mine_page.dart b/lib/pages/main_bottom/mine_page.dart index d1e49ea..144f13c 100644 --- a/lib/pages/main_bottom/mine_page.dart +++ b/lib/pages/main_bottom/mine_page.dart @@ -697,7 +697,7 @@ class _MinePageState extends State { mainAxisSize: MainAxisSize.max, children: [ Text( - 'V1.0.2506.03', + 'V1.0.2506.06', style: FlutterFlowTheme.of(context) .bodyMedium