websocket 在线列表
This commit is contained in:
parent
7d62522f7e
commit
206a5a47a0
@ -23,23 +23,26 @@ export default class Entanglement {
|
|||||||
ws.onclose = async () => {
|
ws.onclose = async () => {
|
||||||
console.log('websocket 连接关闭, 3s后尝试重新连接...')
|
console.log('websocket 连接关闭, 3s后尝试重新连接...')
|
||||||
await new Promise(resolve => setTimeout(resolve, 3000))
|
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||||
this.ws = this.__create_websocket()
|
this.ws = await this.__create_websocket()
|
||||||
}
|
}
|
||||||
ws.onerror = async () => {
|
ws.onerror = async () => {
|
||||||
console.log('websocket 连接错误, 3s后尝试重新连接...')
|
console.log('websocket 连接错误, 3s后尝试重新连接...')
|
||||||
await new Promise(resolve => setTimeout(resolve, 3000))
|
await new Promise(resolve => setTimeout(resolve, 3000))
|
||||||
this.ws = this.__create_websocket()
|
this.ws = await this.__create_websocket()
|
||||||
}
|
}
|
||||||
ws.onmessage = async event => {
|
ws.onmessage = async event => {
|
||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
//console.log('收到消息', data)
|
|
||||||
if (data.type === 'list') {
|
if (data.type === 'list') {
|
||||||
console.debug('收到在线列表', data.list)
|
console.debug('收到在线列表', data.list)
|
||||||
this.users = Promise.all(data.list.map(async user => {
|
this.users = Promise.all(data.list.map(async user => {
|
||||||
console.debug('发送给', user.name, 'offer')
|
console.debug('发送给', user.name, 'offer')
|
||||||
const pc = this.__create_webrtc()
|
const pc = await this.__create_webrtc()
|
||||||
const offer = await pc.createOffer()
|
const offer = await pc.createOffer()
|
||||||
pc.setLocalDescription(offer)
|
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 }))
|
this.ws.send(JSON.stringify({ type: 'offer', user, offer }))
|
||||||
return { ...user, webrtc: pc }
|
return { ...user, webrtc: pc }
|
||||||
}))
|
}))
|
||||||
@ -80,7 +83,7 @@ export default class Entanglement {
|
|||||||
return ws
|
return ws
|
||||||
}
|
}
|
||||||
|
|
||||||
__create_webrtc() {
|
async __create_webrtc() {
|
||||||
const pc = new RTCPeerConnection(this.options)
|
const pc = new RTCPeerConnection(this.options)
|
||||||
pc.onicecandidate = (event) => {
|
pc.onicecandidate = (event) => {
|
||||||
if (event.candidate) {
|
if (event.candidate) {
|
||||||
|
Loading…
Reference in New Issue
Block a user