更新websocket重连

This commit is contained in:
wyf
2025-06-06 10:54:11 +08:00
parent f242d8116a
commit 45b0d8ccb9
2 changed files with 41 additions and 7 deletions

View File

@@ -30,7 +30,8 @@ class InstantBodyPage extends StatefulWidget {
State<InstantBodyPage> createState() => _InstantBodyPageState();
}
class _InstantBodyPageState extends State<InstantBodyPage> {
class _InstantBodyPageState extends State<InstantBodyPage>
with WidgetsBindingObserver {
GlobalController globalController = Get.find();
UserInfoController userInfoController = Get.find();
BlueteethBindController blueteethBindController = Get.find();
@@ -57,27 +58,53 @@ class _InstantBodyPageState extends State<InstantBodyPage> {
@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<InstantBodyPage> {
),
);
}
void _closeWebSocket() {
// 取消WebSocket订阅
edm.EasyDartModule.websocket.sendData(
jsonEncode(WebSocketMessage(path: "/vsbs/web/rt/marttress", type: 2)));
_onlineTimer?.cancel();
}
}
class SpeedControlledGif extends StatefulWidget {

View File

@@ -697,7 +697,7 @@ class _MinePageState extends State<MinePage> {
mainAxisSize: MainAxisSize.max,
children: [
Text(
'V1.0.2506.03',
'V1.0.2506.06',
style:
FlutterFlowTheme.of(context)
.bodyMedium