icecandidate
This commit is contained in:
parent
206a5a47a0
commit
526291bde8
3
index.js
3
index.js
@ -98,8 +98,9 @@ app.ws('/entanglement', (ws, req) => {
|
|||||||
ws.on('message', message => {
|
ws.on('message', message => {
|
||||||
console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size)
|
console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
const data = JSON.parse(message)
|
const data = JSON.parse(message)
|
||||||
|
console.log('icecandidate:', data)
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
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 } }))
|
client.send(JSON.stringify({ ...data, user: { id: ws.id, name: ws.name } }))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -19,7 +19,12 @@ export default class Entanglement {
|
|||||||
const host = window.location.host
|
const host = window.location.host
|
||||||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
||||||
const ws = new WebSocket(`${protocol}://${host}/entanglement?name=sato&channel=chat`)
|
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 () => {
|
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))
|
||||||
@ -34,15 +39,11 @@ export default class Entanglement {
|
|||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.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 = await Promise.all(data.list.map(async user => {
|
||||||
console.debug('发送给', user.name, 'offer')
|
console.debug('发送给', user.name, 'offer')
|
||||||
const pc = await this.__create_webrtc()
|
const pc = await this.__create_webrtc(user)
|
||||||
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 }
|
||||||
}))
|
}))
|
||||||
@ -58,7 +59,7 @@ export default class Entanglement {
|
|||||||
}
|
}
|
||||||
if (data.type === 'offer') {
|
if (data.type === 'offer') {
|
||||||
console.debug(data.user.name, '发来 offer')
|
console.debug(data.user.name, '发来 offer')
|
||||||
const pc = this.__create_webrtc()
|
const pc = await this.__create_webrtc(data.user)
|
||||||
pc.setRemoteDescription(data.offer)
|
pc.setRemoteDescription(data.offer)
|
||||||
const answer = await pc.createAnswer()
|
const answer = await pc.createAnswer()
|
||||||
await pc.setLocalDescription(answer)
|
await pc.setLocalDescription(answer)
|
||||||
@ -83,11 +84,12 @@ export default class Entanglement {
|
|||||||
return ws
|
return ws
|
||||||
}
|
}
|
||||||
|
|
||||||
async __create_webrtc() {
|
async __create_webrtc(user) {
|
||||||
const pc = new RTCPeerConnection(this.options)
|
const pc = new RTCPeerConnection(this.options)
|
||||||
pc.onicecandidate = (event) => {
|
pc.onicecandidate = (event) => {
|
||||||
if (event.candidate) {
|
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) => {
|
pc.oniceconnectionstatechange = (event) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user