feat(connection): 支持服务端下发 TURN 配置与触摸模式协商

This commit is contained in:
tongtong committed 2026-11-09 19:03:55 +08:00
1 parent e06e12fbbc
commit 004bf6b137
6 files changed
+189 -108

No files matched your search

@@ -103,6 +103,7 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
_disposed = true;
signaling.onMessage = null;
signaling.onError = null;
signaling.onConnectionChanged = null;
signaling.dispose();
final orchestrator = _orchestrator;
_orchestrator = null;
@@ -155,24 +156,30 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
_onReport(report);
};
void endSession(String reason) {
if (_disposed) return;
unawaited(disconnect(errorMessage: reason));
}
signaling.onMessage = (message) {
// 先透传给 WebRTC 编排器处理(ANSWER/ICE 等),确保握手能继续
_orchestrator?.handleSignal(message);
// 仅"明确被拒"才进入错误态;TARGET_OFFLINE 可能因握手时序/目标会话短暂未就绪
// 而误报(如设备端刚连上信令、正在应答),不立即判定失败,避免闪断"设备不在线"。
if (message.type == SignalType.connectionRejected) {
state = ConnectionSessionState(
state: ConnectionState.error,
errorMessage: '连接被拒绝',
);
switch (message.type) {
case SignalType.connectionRejected:
endSession('连接被拒绝');
case SignalType.targetOffline:
endSession('设备信令未上线,请确认远程协助已开启');
case SignalType.sessionEnded:
if (message.fromDeviceId == 'server') endSession('远程协助已结束');
default:
final handling = _orchestrator?.handleSignal(message);
if (handling != null) {
unawaited(handling.catchError((Object error) => endSession('信令协商失败')));
}
}
};
signaling.onError = (error) {
state = ConnectionSessionState(
state: ConnectionState.error,
errorMessage: error,
);
signaling.onConnectionChanged = (connected) {
if (!connected) endSession('信令连接已关闭');
};
signaling.onError = endSession;
try {
await signaling.connect(signalServerUrl, targetSn: sn);
@@ -180,10 +187,7 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
await _orchestrator?.start();
} catch (e) {
if (_disposed) return;
state = ConnectionSessionState(
state: ConnectionState.error,
errorMessage: '连接失败: $e',
);
endSession('连接失败: $e');
}
}
@@ -213,6 +217,8 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
}
}
TouchMode get touchMode => _orchestrator?.touchMode ?? TouchMode.legacy;
/// 发送点击(相对坐标 0~1)。
void sendTap(double relX, double relY) {
debugPrint(
@@ -235,7 +241,7 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
_orchestrator?.sendSwipe(relX1, relY1, relX2, relY2);
}
/// 发送实时触摸事件(MOTION_EVENT,跟手用)。action:0=DOWN,1=UP,2=MOVE。
/// 发送实时触摸事件。action 取值为 0=DOWN、1=UP、2=MOVE、3=CANCEL。
void sendMotionEvent(int action, double relX, double relY) {
debugPrint(
'[touch] MOTION action=$action rel=(${relX.toStringAsFixed(3)}, ${relY.toStringAsFixed(3)})',
@@ -311,12 +317,13 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
}
/// 关闭并清理。
Future<void> disconnect() async {
Future<void> disconnect({String? errorMessage}) async {
final signaling = ref.read(signalingClientProvider);
final orchestrator = _orchestrator;
_orchestrator = null;
signaling.onMessage = null;
signaling.onError = null;
signaling.onConnectionChanged = null;
signaling.dispose();
if (orchestrator != null) {
orchestrator.onConnectionState = null;
@@ -325,7 +332,10 @@ class ConnectionController extends Notifier<ConnectionSessionState> {
await _videoRecorder.dispose();
await orchestrator?.dispose();
if (!_disposed) {
state = const ConnectionSessionState(state: ConnectionState.ended);
state = ConnectionSessionState(
state: errorMessage == null ? ConnectionState.ended : ConnectionState.error,
errorMessage: errorMessage,
);
}
}
}