Commit 21a37bfd by tanghuan

测试iOS中Http服务的问题

1 parent d0ea9762
......@@ -1052,18 +1052,52 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
_sendResponse(resp);
}
bool _hasBeenResumed = false;
@override
Future<void> didChangeAppLifecycleState(AppLifecycleState lifecycleState) async {
if (lifecycleState == AppLifecycleState.resumed) {
// 检测本机Http服务是否运行
var localServerService = getIt.get<LocalServerService>();
var result = await localServerService.checkServerHealth();
if (result) {
print('Http服务正常');
if (Platform.isIOS) {
if (_hasBeenResumed) {
// 代表从后台恢复
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();
}
} else {
// 代表初次
_hasBeenResumed = true;
}
} else {
print('Http服务异常---------------------------------------');
emit(state.copyWith(errMsg: '${DateTime.now()}'));
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();
}
}
}
......
......@@ -223,4 +223,40 @@ class LocalServerService {
return false;
}
}
///
/// 在之前启动的端口,重新启动服务
///
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();
}
});
return server;
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!