websocket 在线列表

This commit is contained in:
2023-10-06 14:36:27 +08:00
parent 7d62522f7e
commit 206a5a47a0
1 changed files with 8 additions and 5 deletions

View File

@ -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) {