更新websocket重连
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user