Commit 0adb12b7 by tanghuan

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

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