Commit a242c47d by tanghuan

监测ios白屏问题

1 parent 35706230
...@@ -63,6 +63,9 @@ class WebState extends Equatable { ...@@ -63,6 +63,9 @@ class WebState extends Equatable {
final String h5Version; final String h5Version;
/// 用于测试监测问题
final String testMsg;
const WebState({ const WebState({
this.selectedIndex = 0, this.selectedIndex = 0,
this.loaded = false, this.loaded = false,
...@@ -87,6 +90,7 @@ class WebState extends Equatable { ...@@ -87,6 +90,7 @@ class WebState extends Equatable {
this.chooseVideoCmdFlag = false, this.chooseVideoCmdFlag = false,
this.chooseVideoCmdMessage = '', this.chooseVideoCmdMessage = '',
this.h5Version = '', this.h5Version = '',
this.testMsg = '',
}); });
WebState copyWith({ WebState copyWith({
...@@ -114,6 +118,7 @@ class WebState extends Equatable { ...@@ -114,6 +118,7 @@ class WebState extends Equatable {
bool? chooseVideoCmdFlag, bool? chooseVideoCmdFlag,
String? chooseVideoCmdMessage, String? chooseVideoCmdMessage,
String? h5Version, String? h5Version,
String? testMsg,
}) { }) {
return WebState( return WebState(
selectedIndex: selectedIndex ?? this.selectedIndex, selectedIndex: selectedIndex ?? this.selectedIndex,
...@@ -139,6 +144,7 @@ class WebState extends Equatable { ...@@ -139,6 +144,7 @@ class WebState extends Equatable {
chooseVideoCmdFlag: chooseVideoCmdFlag ?? this.chooseVideoCmdFlag, chooseVideoCmdFlag: chooseVideoCmdFlag ?? this.chooseVideoCmdFlag,
chooseVideoCmdMessage: chooseVideoCmdMessage ?? this.chooseVideoCmdMessage, chooseVideoCmdMessage: chooseVideoCmdMessage ?? this.chooseVideoCmdMessage,
h5Version: h5Version ?? this.h5Version, h5Version: h5Version ?? this.h5Version,
testMsg: testMsg ?? this.testMsg,
); );
} }
...@@ -167,6 +173,7 @@ class WebState extends Equatable { ...@@ -167,6 +173,7 @@ class WebState extends Equatable {
chooseVideoCmdFlag, chooseVideoCmdFlag,
chooseVideoCmdMessage, chooseVideoCmdMessage,
h5Version, h5Version,
testMsg,
]; ];
} }
...@@ -1045,6 +1052,36 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver { ...@@ -1045,6 +1052,36 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_sendResponse(resp); _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:用于标识是初次启动,还是从后台恢复 /// iOS:用于标识是初次启动,还是从后台恢复
/// ///
...@@ -1057,17 +1094,10 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver { ...@@ -1057,17 +1094,10 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
if (lifecycleState == AppLifecycleState.resumed) { if (lifecycleState == AppLifecycleState.resumed) {
if (_hasBeenResumed) { if (_hasBeenResumed) {
// 代表从后台恢复 // 代表从后台恢复
var localServerService = getIt.get<LocalServerService>(); // 1 检测本机Http服务
var result = await localServerService.checkServerHealth(); _checkLocalServerStatus();
if (!result) { // 2 检测WebView状态
try { _checkWebViewStatus();
// 安全起见,执行一次关闭旧的server,
_server?.close();
} catch (e) {
print('SocketException: $e');
}
_server = await localServerService.restartLocalServer();
}
} else { } else {
// 代表初次 // 代表初次
_hasBeenResumed = true; _hasBeenResumed = true;
......
...@@ -138,7 +138,7 @@ class WebPage extends StatelessWidget { ...@@ -138,7 +138,7 @@ class WebPage extends StatelessWidget {
AppBar _buildAppBar(BuildContext ctx, WebState state) { AppBar _buildAppBar(BuildContext ctx, WebState state) {
return AppBar( 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, centerTitle: true,
automaticallyImplyLeading: false, automaticallyImplyLeading: false,
backgroundColor: Color(state.bgColor), 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!