DEBUG: 维持通道

This commit is contained in:
2023-10-03 02:15:29 +08:00
parent 8c6b1d3dbb
commit 8bf2f878cb
1 changed files with 14 additions and 17 deletions

View File

@ -24,15 +24,6 @@ export default class ClientList {
credential: 'x-password' credential: 'x-password'
}] }]
}) })
webrtc.onicecandidate = event => {
if (event.candidate) {
websocket.send(JSON.stringify({
type: 'candidate',
id: data.id,
candidate: event.candidate
}))
}
}
webrtc.ondatachannel = ({ channel }) => { webrtc.ondatachannel = ({ channel }) => {
console.log('对方建立', channel.label, '数据通道') console.log('对方建立', channel.label, '数据通道')
const client = this.clientlist.find(x => x.id === data.id) const client = this.clientlist.find(x => x.id === data.id)
@ -62,15 +53,21 @@ export default class ClientList {
} }
} }
} }
webrtc.onicecandidate = event => {
if (event.candidate) {
websocket.send(JSON.stringify({
type: 'candidate',
id: data.id,
candidate: event.candidate
}))
}
}
webrtc.oniceconnectionstatechange = async event => { webrtc.oniceconnectionstatechange = async event => {
if (webrtc.iceConnectionState === 'disconnected') { if (webrtc.iceConnectionState === 'disconnected' || webrtc.iceConnectionState === 'failed') {
console.log(data.name, 'WebRTC ICE 连接断开 disconnected') console.log('需要添加新的 candidate')
if (this.clientlist.find(x => x.id === data.id)) { // 添加新的 candidate
console.log(data.name, '仍在线, 尝试重连...') } else if (webrtc.iceConnectionState === 'connected' || webrtc.iceConnectionState === 'completed') {
const offer = await webrtc.createOffer() console.log('WebRTC 连接已经建立成功');
await webrtc.setLocalDescription(offer)
websocket.send(JSON.stringify({ type: 'offer', id: data.id, offer }))
}
} }
} }
const channels = Object.entries(this.channels).map(([name, callback]) => { const channels = Object.entries(this.channels).map(([name, callback]) => {