fix: 优化网页端不能连接的问题
This commit is contained in:
@@ -13,9 +13,9 @@ export const DEFAULT_ICE_SERVERS = [
|
||||
// { urls: 'turn:192.168.100.224:3478?transport=tcp', username: 'tt', credential: 'fht' },
|
||||
{ urls: 'stun:175.178.213.60:3478' },
|
||||
// { urls: 'stun:192.168.5.224:3478' },
|
||||
// { urls: 'stun:stun.l.google.com:19302' },
|
||||
// { urls: 'stun:stun1.l.google.com:19302' },
|
||||
// { urls: 'stun:stun2.l.google.com:19302' },
|
||||
// { urls: 'stun:stun.l.google.com:19302' },
|
||||
// { urls: 'stun:stun1.l.google.com:19302' },
|
||||
// { urls: 'stun:stun2.l.google.com:19302' },
|
||||
];
|
||||
|
||||
export const store = reactive({
|
||||
@@ -128,21 +128,30 @@ export async function connectToDevice(targetId) {
|
||||
|
||||
if (webrtc) { await webrtc.close(); webrtc = null; }
|
||||
|
||||
webrtc = new WebRtcController({
|
||||
iceServers: store.iceServers,
|
||||
deviceId: store.deviceId,
|
||||
targetDeviceId: targetId,
|
||||
signaling,
|
||||
onIceState: (s) => {
|
||||
if (s === 'connected') { store.rtcConnected = true; store.statusText = '已连接,可远程控制'; }
|
||||
else if (s === 'disconnected' || s === 'failed') { store.rtcConnected = false; store.statusText = '连接已断开'; }
|
||||
},
|
||||
onDataChannelState: (open) => { store.dataChannelOpen = open; },
|
||||
onStream: (stream) => { store.remoteStream = markRaw(stream); },
|
||||
onStats: (stats) => { store.stats = stats; },
|
||||
onError: (msg) => { if (msg) store.error = msg; },
|
||||
});
|
||||
await webrtc.createOffer();
|
||||
try {
|
||||
webrtc = new WebRtcController({
|
||||
iceServers: store.iceServers,
|
||||
deviceId: store.deviceId,
|
||||
targetDeviceId: targetId,
|
||||
signaling,
|
||||
onIceState: (s) => {
|
||||
if (s === 'connected') { store.rtcConnected = true; store.statusText = '已连接,可远程控制'; }
|
||||
else if (s === 'disconnected' || s === 'failed') { store.rtcConnected = false; store.statusText = '连接已断开'; }
|
||||
},
|
||||
onDataChannelState: (open) => { store.dataChannelOpen = open; },
|
||||
onStream: (stream) => { store.remoteStream = markRaw(stream); },
|
||||
onStats: (stats) => { store.stats = stats; },
|
||||
onError: (msg) => { if (msg) store.error = msg; },
|
||||
});
|
||||
await webrtc.createOffer();
|
||||
} catch (e) {
|
||||
// 捕获 createOffer 阶段的所有异常,避免未处理的 Promise 拒绝导致浏览器中断脚本执行、
|
||||
// 间接影响 WebSocket 信令连接的存活。
|
||||
console.error('[connectToDevice] 创建 Offer 失败:', e);
|
||||
store.error = '创建连接失败: ' + (e?.message || e);
|
||||
store.statusText = '连接失败';
|
||||
if (webrtc) { await webrtc.close(); webrtc = null; }
|
||||
}
|
||||
}
|
||||
|
||||
export async function disconnectDevice() {
|
||||
|
||||
Reference in New Issue
Block a user