feat: 目标不在线时进行提示和更新UI

This commit is contained in:
2026-07-14 16:40:41 +08:00
parent 67c7e48190
commit d43c0331b6
4 changed files with 80 additions and 4 deletions

View File

@@ -30,6 +30,9 @@ class RemoteController {
/// ICE 连接断开(用于提示用户并返回连接设置)。
void Function(String message)? onIceDisconnected;
/// 目标被控端不在线(服务器回送 TARGET_OFFLINE用于提示用户并复位 UI
void Function(String message)? onTargetOffline;
/// 远端视频渲染器就绪。
void Function(RTCVideoRenderer renderer)? onRemoteStream;
@@ -92,6 +95,11 @@ class RemoteController {
final payload = jsonDecode(message.payload!) as Map<String, dynamic>;
_webRtc?.handleIceCandidate(payload);
break;
case 'TARGET_OFFLINE':
final text = message.payload ?? '目标被控端不在线,请确认设备已开启并连接服务器';
onStatusChanged?.call('状态: $text');
onTargetOffline?.call(text);
break;
}
}

View File

@@ -99,6 +99,9 @@ class _ControllerHomeState extends State<ControllerHome> {
_controller!.onIceDisconnected = (message) {
_onIceDisconnected(message);
};
_controller!.onTargetOffline = (message) {
_onTargetOffline(message);
};
_controller!.onRemoteStream = (renderer) {
setState(() => _renderer = renderer);
renderer.addListener(_onRendererUpdate);
@@ -130,6 +133,20 @@ class _ControllerHomeState extends State<ControllerHome> {
await _disconnect();
}
/// 目标被控端不在线:提示用户并复位到连接设置面板。
Future<void> _onTargetOffline(String message) async {
final scaffoldCtx = _scaffoldKey.currentState?.context;
if (mounted && scaffoldCtx != null) {
ScaffoldMessenger.of(scaffoldCtx).showSnackBar(
SnackBar(
content: Text(message),
duration: const Duration(seconds: 3),
),
);
}
setState(() => _connected = false);
}
Future<void> _disconnect() async {
await _controller?.disconnect();
_controller = null;