feat: ice断开返回配置页面
This commit is contained in:
@@ -43,6 +43,9 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
RemoteController? _controller;
|
||||
RTCVideoRenderer? _renderer;
|
||||
|
||||
/// 用于在任意位置(含回调中)弹出提示。
|
||||
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
||||
|
||||
bool _connected = false;
|
||||
String _status = '状态: 已停止';
|
||||
String _stats = '';
|
||||
@@ -93,6 +96,9 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
setState(() => _connected = false);
|
||||
_setStatus('状态: 远端已断开');
|
||||
};
|
||||
_controller!.onIceDisconnected = (message) {
|
||||
_onIceDisconnected(message);
|
||||
};
|
||||
_controller!.onRemoteStream = (renderer) {
|
||||
setState(() => _renderer = renderer);
|
||||
renderer.addListener(_onRendererUpdate);
|
||||
@@ -110,6 +116,20 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
}
|
||||
}
|
||||
|
||||
/// ICE 断开:提示用户并自动返回连接设置面板。
|
||||
Future<void> _onIceDisconnected(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),
|
||||
),
|
||||
);
|
||||
}
|
||||
await _disconnect();
|
||||
}
|
||||
|
||||
Future<void> _disconnect() async {
|
||||
await _controller?.disconnect();
|
||||
_controller = null;
|
||||
@@ -125,6 +145,7 @@ class _ControllerHomeState extends State<ControllerHome> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
key: _scaffoldKey,
|
||||
appBar: AppBar(title: const Text('WebRTC 控制端')),
|
||||
body: _connected ? _buildControlPanel() : _buildSetupPanel(),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user