Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
ethan
/
appframe
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit 21a37bfd
authored
2025-12-15 17:33:48 +0800
by
tanghuan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
测试iOS中Http服务的问题
1 parent
d0ea9762
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletions
lib/bloc/web_cubit.dart
lib/services/local_server_service.dart
lib/bloc/web_cubit.dart
View file @
21a37bf
...
...
@@ -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
>();
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
{
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
();
}
}
}
...
...
lib/services/local_server_service.dart
View file @
21a37bf
...
...
@@ -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
;
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment