From 526291bde855cff63a3352b0ee2456418a11cbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Fri, 6 Oct 2023 15:09:52 +0800 Subject: [PATCH] icecandidate --- index.js | 3 ++- public/entanglement.js | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 20609ab..085edfb 100644 --- a/index.js +++ b/index.js @@ -98,8 +98,9 @@ app.ws('/entanglement', (ws, req) => { ws.on('message', message => { console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size) const data = JSON.parse(message) + console.log('icecandidate:', data) wsInstance.getWss().clients.forEach(client => { - if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === data.id) { + if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === data.user.id) { client.send(JSON.stringify({ ...data, user: { id: ws.id, name: ws.name } })) } }) diff --git a/public/entanglement.js b/public/entanglement.js index a446b44..d1f26d0 100644 --- a/public/entanglement.js +++ b/public/entanglement.js @@ -19,7 +19,12 @@ export default class Entanglement { const host = window.location.host const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws' const ws = new WebSocket(`${protocol}://${host}/entanglement?name=sato&channel=chat`) - ws.onopen = () => console.log('websocket 连接成功') + ws.onopen = async () => { + console.log('websocket 连接成功') + if (this.ws instanceof Promise) { + this.ws = await this.ws + } + } ws.onclose = async () => { console.log('websocket 连接关闭, 3s后尝试重新连接...') await new Promise(resolve => setTimeout(resolve, 3000)) @@ -34,15 +39,11 @@ export default class Entanglement { const data = JSON.parse(event.data) if (data.type === 'list') { console.debug('收到在线列表', data.list) - this.users = Promise.all(data.list.map(async user => { + this.users = await Promise.all(data.list.map(async user => { console.debug('发送给', user.name, 'offer') - const pc = await this.__create_webrtc() + const pc = await this.__create_webrtc(user) const offer = await pc.createOffer() pc.setLocalDescription(offer) - // 判断this.ws是否为Promise - if (this.ws instanceof Promise) { - this.ws = await this.ws - } this.ws.send(JSON.stringify({ type: 'offer', user, offer })) return { ...user, webrtc: pc } })) @@ -58,7 +59,7 @@ export default class Entanglement { } if (data.type === 'offer') { console.debug(data.user.name, '发来 offer') - const pc = this.__create_webrtc() + const pc = await this.__create_webrtc(data.user) pc.setRemoteDescription(data.offer) const answer = await pc.createAnswer() await pc.setLocalDescription(answer) @@ -83,11 +84,12 @@ export default class Entanglement { return ws } - async __create_webrtc() { + async __create_webrtc(user) { const pc = new RTCPeerConnection(this.options) pc.onicecandidate = (event) => { if (event.candidate) { - this.ws.send(JSON.stringify({ type: 'icecandidate', user: this.account, icecandidate: event.candidate })) + console.debug(user.name, '发出 icecandidate 候选通道') + this.ws.send(JSON.stringify({ type: 'icecandidate', user, icecandidate: event.candidate })) } } pc.oniceconnectionstatechange = (event) => {