更新websocket重连
This commit is contained in:
@@ -30,7 +30,8 @@ class InstantBodyPage extends StatefulWidget {
|
|||||||
State<InstantBodyPage> createState() => _InstantBodyPageState();
|
State<InstantBodyPage> createState() => _InstantBodyPageState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _InstantBodyPageState extends State<InstantBodyPage> {
|
class _InstantBodyPageState extends State<InstantBodyPage>
|
||||||
|
with WidgetsBindingObserver {
|
||||||
GlobalController globalController = Get.find();
|
GlobalController globalController = Get.find();
|
||||||
UserInfoController userInfoController = Get.find();
|
UserInfoController userInfoController = Get.find();
|
||||||
BlueteethBindController blueteethBindController = Get.find();
|
BlueteethBindController blueteethBindController = Get.find();
|
||||||
@@ -57,27 +58,53 @@ class _InstantBodyPageState extends State<InstantBodyPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
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(
|
edm.EasyDartModule.websocket.sendData(jsonEncode(WebSocketMessage(
|
||||||
path: "/vsbs/web/rt/marttress",
|
path: "/vsbs/web/rt/marttress",
|
||||||
type: 1,
|
type: 1,
|
||||||
data: {"mac": widget.personInfo['mac']})));
|
data: {"mac": widget.personInfo['mac']})));
|
||||||
_startOnlineTimer(); // 初始化时启动定时器
|
_startOnlineTimer();
|
||||||
super.initState();
|
|
||||||
}
|
}
|
||||||
|
//y
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_onlineTimer?.cancel(); // 取消定时器,防止内存泄漏
|
WidgetsBinding.instance.removeObserver(this); // 移除生命周期观察者
|
||||||
edm.EasyDartModule.websocket.sendData(
|
_onlineTimer?.cancel();
|
||||||
jsonEncode(WebSocketMessage(path: "/vsbs/web/rt/marttress", type: 2)));
|
_closeWebSocket();
|
||||||
super.dispose();
|
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() {
|
void _startOnlineTimer() {
|
||||||
_onlineTimer?.cancel(); // 取消之前的定时器
|
_onlineTimer?.cancel(); // 取消之前的定时器
|
||||||
_onlineTimer = Timer.periodic(Duration(seconds: 30), (timer) {
|
_onlineTimer = Timer.periodic(Duration(seconds: 30), (timer) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
setState(() {
|
setState(() {
|
||||||
|
edm.EasyDartModule.logger.info("30 秒内没有接收到数据,设置为离线");
|
||||||
onlineState = "离线".tr; // 30 秒内没有接收到数据,设置为离线
|
onlineState = "离线".tr; // 30 秒内没有接收到数据,设置为离线
|
||||||
inBed = "未知数据".tr;
|
inBed = "未知数据".tr;
|
||||||
bodyMotion = -1;
|
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 {
|
class SpeedControlledGif extends StatefulWidget {
|
||||||
|
|||||||
@@ -697,7 +697,7 @@ class _MinePageState extends State<MinePage> {
|
|||||||
mainAxisSize: MainAxisSize.max,
|
mainAxisSize: MainAxisSize.max,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
'V1.0.2506.03',
|
'V1.0.2506.06',
|
||||||
style:
|
style:
|
||||||
FlutterFlowTheme.of(context)
|
FlutterFlowTheme.of(context)
|
||||||
.bodyMedium
|
.bodyMedium
|
||||||
|
|||||||
Reference in New Issue
Block a user