From c7c3ff2b24be7bd018eda5f6fc5f0729d22d9f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Tue, 3 Oct 2023 01:27:00 +0800 Subject: [PATCH] =?UTF-8?q?webrtc=E6=96=AD=E5=BC=80=E6=97=B6=E9=87=8D?= =?UTF-8?q?=E8=BF=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/client.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/public/client.js b/public/client.js index b499055..99491c2 100644 --- a/public/client.js +++ b/public/client.js @@ -62,8 +62,18 @@ export default class ClientList { } } } - webrtc.oniceconnectionstatechange = event => { - console.log('WebRTC ICE 连接状态更改:', webrtc.iceConnectionState) + webrtc.oniceconnectionstatechange = async event => { + console.log('WebRTC ICE 连接状态更改:', webrtc.iceConnectionState, event) + if (webrtc.iceConnectionState === 'disconnected') { + console.log('WebRTC ICE connection disconnected') + // 如果用户没有离线, 则尝试重连 + if (this.clientlist.find(x => x.id === data.id)) { + console.log('用户仍在线, 尝试重连...') + const offer = await webrtc.createOffer() + await webrtc.setLocalDescription(offer) + websocket.send(JSON.stringify({ type: 'offer', id: data.id, offer })) + } + } } const channels = Object.entries(this.channels).map(([name, callback]) => { const channel = webrtc.createDataChannel(name, { reliable: true })