Commit a242c47d by tanghuan

监测ios白屏问题

1 parent 35706230
......@@ -63,6 +63,9 @@ class WebState extends Equatable {
final String h5Version;
/// 用于测试监测问题
final String testMsg;
const WebState({
this.selectedIndex = 0,
this.loaded = false,
......@@ -87,6 +90,7 @@ class WebState extends Equatable {
this.chooseVideoCmdFlag = false,
this.chooseVideoCmdMessage = '',
this.h5Version = '',
this.testMsg = '',
});
WebState copyWith({
......@@ -114,6 +118,7 @@ class WebState extends Equatable {
bool? chooseVideoCmdFlag,
String? chooseVideoCmdMessage,
String? h5Version,
String? testMsg,
}) {
return WebState(
selectedIndex: selectedIndex ?? this.selectedIndex,
......@@ -139,6 +144,7 @@ class WebState extends Equatable {
chooseVideoCmdFlag: chooseVideoCmdFlag ?? this.chooseVideoCmdFlag,
chooseVideoCmdMessage: chooseVideoCmdMessage ?? this.chooseVideoCmdMessage,
h5Version: h5Version ?? this.h5Version,
testMsg: testMsg ?? this.testMsg,
);
}
......@@ -167,6 +173,7 @@ class WebState extends Equatable {
chooseVideoCmdFlag,
chooseVideoCmdMessage,
h5Version,
testMsg,
];
}
......@@ -1045,6 +1052,36 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_sendResponse(resp);
}
Future<void> _checkLocalServerStatus() async {
var localServerService = getIt.get<LocalServerService>();
var result = await localServerService.checkServerHealth();
if (!result) {
try {
// 安全起见,执行一次关闭旧的server,
_server?.close();
} catch (e) {
print('SocketException: $e');
}
emit(state.copyWith(testMsg: 'A ${DateTime.now()}'));
_server = await localServerService.restartLocalServer();
}
}
Future<void> _checkWebViewStatus() async {
try {
// 尝试获取页面标题来验证 WebView 是否响应
final title = await controller.runJavaScriptReturningResult('document.title');
if (title.toString().isEmpty) {
emit(state.copyWith(testMsg: 'C ${DateTime.now()}'));
_loadHtml();
}
} catch (e) {
// JavaScript 执行失败,可能 WebView 已失效
emit(state.copyWith(testMsg: 'B ${DateTime.now()}'));
_loadHtml();
}
}
///
/// iOS:用于标识是初次启动,还是从后台恢复
///
......@@ -1057,17 +1094,10 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
if (lifecycleState == AppLifecycleState.resumed) {
if (_hasBeenResumed) {
// 代表从后台恢复
var localServerService = getIt.get<LocalServerService>();
var result = await localServerService.checkServerHealth();
if (!result) {
try {
// 安全起见,执行一次关闭旧的server,
_server?.close();
} catch (e) {
print('SocketException: $e');
}
_server = await localServerService.restartLocalServer();
}
// 1 检测本机Http服务
_checkLocalServerStatus();
// 2 检测WebView状态
_checkWebViewStatus();
} else {
// 代表初次
_hasBeenResumed = true;
......
......@@ -138,7 +138,7 @@ class WebPage extends StatelessWidget {
AppBar _buildAppBar(BuildContext ctx, WebState state) {
return AppBar(
title: Text(state.title, style: TextStyle(color: Color(state.titleColor), fontSize: 18)),
title: Text(state.title + state.testMsg, style: TextStyle(color: Color(state.titleColor), fontSize: 18)),
centerTitle: true,
automaticallyImplyLeading: false,
backgroundColor: Color(state.bgColor),
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!