diff --git a/public/entanglement.js b/public/entanglement.js index 8f3be09..a446b44 100644 --- a/public/entanglement.js +++ b/public/entanglement.js @@ -23,23 +23,26 @@ export default class Entanglement { ws.onclose = async () => { console.log('websocket 连接关闭, 3s后尝试重新连接...') await new Promise(resolve => setTimeout(resolve, 3000)) - this.ws = this.__create_websocket() + this.ws = await this.__create_websocket() } ws.onerror = async () => { console.log('websocket 连接错误, 3s后尝试重新连接...') await new Promise(resolve => setTimeout(resolve, 3000)) - this.ws = this.__create_websocket() + this.ws = await this.__create_websocket() } ws.onmessage = async event => { const data = JSON.parse(event.data) - //console.log('收到消息', data) if (data.type === 'list') { console.debug('收到在线列表', data.list) this.users = Promise.all(data.list.map(async user => { console.debug('发送给', user.name, 'offer') - const pc = this.__create_webrtc() + const pc = await this.__create_webrtc() 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 } })) @@ -80,7 +83,7 @@ export default class Entanglement { return ws } - __create_webrtc() { + async __create_webrtc() { const pc = new RTCPeerConnection(this.options) pc.onicecandidate = (event) => { if (event.candidate) {