Commit 0adb12b7 by tanghuan

去掉针对iOS系统的http服务的排查测试

1 parent 7a9ce324
...@@ -63,9 +63,6 @@ class WebState extends Equatable { ...@@ -63,9 +63,6 @@ class WebState extends Equatable {
final String h5Version; final String h5Version;
// 测试用,传递http服务检测错误消息
final String errMsg;
const WebState({ const WebState({
this.selectedIndex = 0, this.selectedIndex = 0,
this.loaded = false, this.loaded = false,
...@@ -90,7 +87,6 @@ class WebState extends Equatable { ...@@ -90,7 +87,6 @@ class WebState extends Equatable {
this.chooseVideoCmdFlag = false, this.chooseVideoCmdFlag = false,
this.chooseVideoCmdMessage = '', this.chooseVideoCmdMessage = '',
this.h5Version = '', this.h5Version = '',
this.errMsg = '',
}); });
WebState copyWith({ WebState copyWith({
...@@ -118,7 +114,6 @@ class WebState extends Equatable { ...@@ -118,7 +114,6 @@ class WebState extends Equatable {
bool? chooseVideoCmdFlag, bool? chooseVideoCmdFlag,
String? chooseVideoCmdMessage, String? chooseVideoCmdMessage,
String? h5Version, String? h5Version,
String? errMsg,
}) { }) {
return WebState( return WebState(
selectedIndex: selectedIndex ?? this.selectedIndex, selectedIndex: selectedIndex ?? this.selectedIndex,
...@@ -144,7 +139,6 @@ class WebState extends Equatable { ...@@ -144,7 +139,6 @@ 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,
errMsg: errMsg ?? this.errMsg,
); );
} }
...@@ -173,7 +167,6 @@ class WebState extends Equatable { ...@@ -173,7 +167,6 @@ class WebState extends Equatable {
chooseVideoCmdFlag, chooseVideoCmdFlag,
chooseVideoCmdMessage, chooseVideoCmdMessage,
h5Version, h5Version,
errMsg,
]; ];
} }
...@@ -1052,26 +1045,23 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver { ...@@ -1052,26 +1045,23 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_sendResponse(resp); _sendResponse(resp);
} }
///
/// iOS:用于标识是初次启动,还是从后台恢复
///
bool _hasBeenResumed = false; bool _hasBeenResumed = false;
@override @override
Future<void> didChangeAppLifecycleState(AppLifecycleState lifecycleState) async { Future<void> didChangeAppLifecycleState(AppLifecycleState lifecycleState) async {
if (lifecycleState == AppLifecycleState.resumed) { // 只针对iOS进行处理
// 检测本机Http服务是否运行
var localServerService = getIt.get<LocalServerService>();
if (Platform.isIOS) { if (Platform.isIOS) {
if (lifecycleState == AppLifecycleState.resumed) {
if (_hasBeenResumed) { if (_hasBeenResumed) {
// 代表从后台恢复 // 代表从后台恢复
var localServerService = getIt.get<LocalServerService>();
var result = await localServerService.checkServerHealth(); var result = await localServerService.checkServerHealth();
if (result) { if (!result) {
print('Http服务正常');
} else {
print('Http服务异常---------------------------------------');
emit(state.copyWith(errMsg: '${DateTime.now()}'));
try { try {
// 安全起见,执行一次关闭旧的server,
_server?.close(); _server?.close();
} catch (e) { } catch (e) {
print('SocketException: $e'); print('SocketException: $e');
...@@ -1080,24 +1070,8 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver { ...@@ -1080,24 +1070,8 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
} }
} else { } else {
// 代表初次 // 代表初次
_hasBeenResumed = true; _hasBeenResumed = true;
} }
} else {
var result = await localServerService.checkServerHealth();
if (result) {
print('Http服务正常');
} else {
print('Http服务异常---------------------------------------');
emit(state.copyWith(errMsg: '${DateTime.now()}'));
try {
_server?.close();
} catch (e) {
print('SocketException: $e');
}
_server = await localServerService.restartLocalServer();
}
} }
} }
...@@ -1129,12 +1103,12 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver { ...@@ -1129,12 +1103,12 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_server?.close(); _server?.close();
// _fluwx.removeSubscriber(_responseListener); // _fluwx.removeSubscriber(_responseListener);
// 移除观察者
WidgetsBinding.instance.removeObserver(this);
await _playerService?.close(); await _playerService?.close();
await _recorderService?.close(); await _recorderService?.close();
// 移除观察者
WidgetsBinding.instance.removeObserver(this);
return super.close(); return super.close();
} }
} }
...@@ -33,7 +33,12 @@ class LocalServerService { ...@@ -33,7 +33,12 @@ class LocalServerService {
} }
} }
server.listen((HttpRequest request) async { server.listen(_onData);
return server;
}
Future<void> _onData(HttpRequest request) async {
final String requestPath = request.uri.path == '/' ? '/index.html' : request.uri.path; final String requestPath = request.uri.path == '/' ? '/index.html' : request.uri.path;
try { try {
...@@ -54,9 +59,6 @@ class LocalServerService { ...@@ -54,9 +59,6 @@ class LocalServerService {
..write('Internal server error') ..write('Internal server error')
..close(); ..close();
} }
});
return server;
} }
// 目录下的文件 // 目录下的文件
...@@ -212,10 +214,10 @@ class LocalServerService { ...@@ -212,10 +214,10 @@ class LocalServerService {
final response = await request.close(); final response = await request.close();
if (response.statusCode == HttpStatus.ok) { if (response.statusCode == HttpStatus.ok) {
print('本地服务器健康检查: 正常'); //print('本地服务器健康检查: 正常');
return true; return true;
} else { } else {
print('本地服务器健康检查: 异常,状态码 ${response.statusCode}'); //print('本地服务器健康检查: 异常,状态码 ${response.statusCode}');
return false; return false;
} }
} catch (e) { } catch (e) {
...@@ -228,35 +230,9 @@ class LocalServerService { ...@@ -228,35 +230,9 @@ class LocalServerService {
/// 在之前启动的端口,重新启动服务 /// 在之前启动的端口,重新启动服务
/// ///
Future<HttpServer> restartLocalServer() async { Future<HttpServer> restartLocalServer() async {
// 使用之前保存的端口 Constant.localServerPort 进行启动 // 使用之前保存的端口 Constant.localServerPort 进行启动
HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, Constant.localServerPort); HttpServer server = await HttpServer.bind(InternetAddress.loopbackIPv4, Constant.localServerPort);
server.listen(_onData);
server.listen((HttpRequest request) async {
final String requestPath = request.uri.path == '/' ? '/index.html' : request.uri.path;
try {
if (requestPath.startsWith('${Constant.localServerTemp}/')) {
// 目录文件服务逻辑
await _serveTempFile(request, requestPath);
} else if (requestPath.startsWith('${Constant.localServerTest}/')) {
// 内部assets文件服务逻辑
await _serveAssetFile(request, requestPath);
} else {
// 内部集成H5文件服务逻辑
await _serveHttpFile(request, requestPath);
}
} catch (e) {
print('处理请求时出错: $e');
request.response
..statusCode = HttpStatus.internalServerError
..write('Internal server error')
..close();
}
});
return server; return server;
} }
} }
...@@ -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 + state.errMsg, style: TextStyle(color: Color(state.titleColor), fontSize: 18)), title: Text(state.title, 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!