webrtc/public/client.js

144 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-09-28 23:49:26 +08:00
import { List, ListItem } from './weigets.js'
export default class ClientList {
2023-09-29 17:31:37 +08:00
constructor({ channels = {}, EventListeners = {} }) {
2023-09-29 20:20:00 +08:00
this.channels = channels
2023-09-29 17:31:37 +08:00
this.EventListeners = EventListeners
2023-09-28 23:49:26 +08:00
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
const host = window.location.host
this.websocket = new WebSocket(`${protocol}://${host}/webrtc/music`)
this.clientlist = []
this.ul = List({})
document.body.appendChild(this.ul)
this.websocket.onmessage = async event => {
const data = JSON.parse(event.data)
2023-09-29 01:12:20 +08:00
const webrtc_init = () => {
2023-09-28 23:49:26 +08:00
const webrtc = new RTCPeerConnection()
2023-09-29 17:31:37 +08:00
Object.entries(channels).forEach(([name, callback]) => {
const channel = webrtc.createDataChannel(name)
channel.onopen = event => {
2023-09-29 20:20:00 +08:00
//console.log('datachannel 已打开', event)
2023-09-29 21:21:26 +08:00
if (callback.onopen) callback.onopen(event)
2023-09-29 17:31:37 +08:00
}
channel.onclose = event => {
2023-09-29 20:20:00 +08:00
//console.log('datachannel 已关闭', event)
2023-09-29 21:21:26 +08:00
if (callback.onclose) callback.onclose(event)
2023-09-29 17:31:37 +08:00
}
channel.onerror = event => {
2023-09-29 20:20:00 +08:00
//console.log('datachannel 发生错误', event)
2023-09-29 21:21:26 +08:00
if (callback.onerror) callback.onerror(event)
2023-09-29 17:31:37 +08:00
}
channel.onmessage = event => {
2023-09-29 20:20:00 +08:00
//console.log('datachannel 收到数据', event)
2023-09-29 21:21:26 +08:00
if (callback.onmessage) callback.onmessage(event)
2023-09-29 17:31:37 +08:00
}
})
2023-09-28 23:54:58 +08:00
webrtc.onicecandidate = event => {
if (event.candidate) {
2023-09-29 17:31:37 +08:00
this.websocket.send(JSON.stringify({
type: 'candidate',
id: data.id,
candidate: event.candidate
}))
2023-09-28 23:54:58 +08:00
}
}
2023-09-29 17:31:37 +08:00
webrtc.ondatachannel = ({ channel }) => {
2023-09-29 21:21:26 +08:00
//console.log('收到对方 datachannel', channel)
2023-09-29 17:31:37 +08:00
channel.onmessage = event => {
2023-09-29 21:21:26 +08:00
//console.log('收到对方 datachannel message', event)
2023-09-29 17:31:37 +08:00
if (channels[event.target.label]) {
channels[event.target.label].onmessage(event, channel)
}
2023-09-29 01:12:20 +08:00
}
}
webrtc.oniceconnectionstatechange = event => {
2023-09-29 20:20:00 +08:00
//console.log('WebRTC ICE 连接状态更改:', webrtc.iceConnectionState)
2023-09-29 01:12:20 +08:00
}
return webrtc
}
if (data.type === 'list') {
2023-09-29 20:20:00 +08:00
//console.log('取得在线对端列表:', data)
2023-09-29 01:12:20 +08:00
const webrtc = webrtc_init()
2023-09-29 20:20:00 +08:00
//console.log('发送给对方 offer')
2023-09-28 23:49:26 +08:00
const offer = await webrtc.createOffer()
await webrtc.setLocalDescription(offer)
this.clientlist.push({ id: data.id, name: data.name, webrtc })
this.websocket.send(JSON.stringify({ type: 'offer', id: data.id, offer }))
2023-09-29 17:31:37 +08:00
this.add(data)
return
2023-09-28 23:49:26 +08:00
}
if (data.type === 'push') {
console.log('新上线客户端:', data)
return this.add(data)
}
if (data.type === 'pull') {
console.log('移除客户端:', data)
return this.remove(data)
}
if (data.type === 'offer') {
2023-09-29 20:20:00 +08:00
//console.log('收到对方 offer', data)
2023-09-29 01:12:20 +08:00
const webrtc = webrtc_init()
2023-09-28 23:49:26 +08:00
this.clientlist.push({ id: data.id, name: data.name, webrtc })
2023-09-29 20:20:00 +08:00
//console.log('发送给对方 answer')
2023-09-28 23:49:26 +08:00
await webrtc.setRemoteDescription(data.offer)
const answer = await webrtc.createAnswer()
await webrtc.setLocalDescription(answer)
this.websocket.send(JSON.stringify({ type: 'answer', id: data.id, answer }))
return
}
if (data.type === 'answer') {
2023-09-29 20:20:00 +08:00
//console.log('收到对方 answer', data)
2023-09-28 23:49:26 +08:00
const pc = this.clientlist.find(client => client.id === data.id).webrtc
await pc.setRemoteDescription(data.answer)
return
}
if (data.type === 'candidate') {
2023-09-29 20:20:00 +08:00
// console.log('收到 candidate 并将其添加到远程端', data.candidate)
2023-09-28 23:49:26 +08:00
const pc = this.clientlist.find(client => client.id === data.id).webrtc
await pc.addIceCandidate(data.candidate)
2023-09-29 20:20:00 +08:00
return
2023-09-28 23:49:26 +08:00
}
console.log('收到未知数据:', data)
}
}
2023-09-29 21:21:26 +08:00
setChannel(name, option) {
this.channels[name] = option
}
2023-09-28 23:49:26 +08:00
add(item) {
this.ul.appendChild(ListItem({
id: item.id,
innerText: item.name ?? item.id,
onclick: event => {
},
chidren: []
}))
}
remove(item) {
this.clientlist = this.clientlist.filter(client => client.id !== item.id)
this.ul.removeChild(document.getElementById(item.id))
}
update(item) { }
get(id) { }
getAll() { }
clear() { }
2023-09-29 01:12:20 +08:00
// 添加回调函数
on(name, callback) {
this.EventListeners[name] = callback
}
// 执行回调函数
_on(name, ...args) {
if (this.EventListeners[name]) {
this.EventListeners[name](...args)
}
}
// 通过指定通道发送数据(广播)
send(name, data) {
console.log('广播数据:', data, '到通道:', name, '到所有客户端')
this.clientlist.forEach(client => {
2023-09-29 17:31:37 +08:00
console.log('发送数据到客户端:', client.id)
2023-09-29 01:12:20 +08:00
const channel = client.webrtc.getDataChannel(name) ?? client.webrtc.createDataChannel(name)
channel.send(data)
})
}
2023-09-28 23:49:26 +08:00
}