feat:处理组件渲染导致重复注册事件文件

This commit is contained in:
MatrixSeven
2025-08-24 15:41:58 +08:00
parent 75825e1104
commit 6b69d35a20
3 changed files with 44 additions and 24 deletions

View File

@@ -289,11 +289,25 @@ export function useSharedWebRTCManager(): WebRTCConnection {
case 'answer':
console.log('[SharedWebRTC] 📬 处理answer...');
if (pc.signalingState === 'have-local-offer') {
await pc.setRemoteDescription(new RTCSessionDescription(message.payload));
console.log('[SharedWebRTC] ✅ answer 处理完成');
} else {
console.warn('[SharedWebRTC] ⚠️ PeerConnection状态不是have-local-offer:', pc.signalingState);
try {
if (pc.signalingState === 'have-local-offer') {
await pc.setRemoteDescription(new RTCSessionDescription(message.payload));
console.log('[SharedWebRTC] ✅ answer 处理完成');
} else {
console.warn('[SharedWebRTC] ⚠️ PeerConnection状态不是have-local-offer:', pc.signalingState);
// 如果状态不对,尝试重新创建 offer
if (pc.connectionState === 'connected' || pc.connectionState === 'connecting') {
console.log('[SharedWebRTC] 🔄 连接状态正常但信令状态异常尝试重新创建offer');
// 这里不直接处理,让连接自然建立
}
}
} catch (error) {
console.error('[SharedWebRTC] ❌ 处理answer失败:', error);
if (error instanceof Error && error.message.includes('Failed to set local answer sdp')) {
console.warn('[SharedWebRTC] ⚠️ Answer处理失败可能是连接状态变化导致的');
// 清理连接状态,让客户端重新连接
updateState({ error: 'WebRTC连接状态异常请重新连接', isPeerConnected: false });
}
}
break;