This commit is contained in:
2023-10-06 16:06:17 +08:00
parent 35cde09bb0
commit d3011d9280
2 changed files with 62 additions and 5 deletions

View File

@ -43,7 +43,7 @@ export default class Entanglement {
console.debug('发送给', user.name, 'offer') console.debug('发送给', user.name, 'offer')
const pc = await this.__create_webrtc(user) const pc = await this.__create_webrtc(user)
const offer = await pc.createOffer() const offer = await pc.createOffer()
pc.setLocalDescription(offer) await pc.setLocalDescription(offer)
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 }
})) }))
@ -60,7 +60,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 = await this.__create_webrtc(data.user) const pc = await this.__create_webrtc(data.user)
pc.setRemoteDescription(data.offer) await pc.setRemoteDescription(data.offer)
const answer = await pc.createAnswer() const answer = await pc.createAnswer()
await pc.setLocalDescription(answer) await pc.setLocalDescription(answer)
this.ws.send(JSON.stringify({ type: 'answer', user: data.user, answer })) this.ws.send(JSON.stringify({ type: 'answer', user: data.user, answer }))
@ -129,12 +129,28 @@ export default class Entanglement {
console.log(data.user.name, '通道', event.channel.label, '发生错误') console.log(data.user.name, '通道', event.channel.label, '发生错误')
} }
} }
// 创建数据通道
const channel = pc.createDataChannel('json', { reliable: true })
channel.onopen = () => {
console.log('打开数据通道')
channel.send(JSON.stringify({ name: 'sato', hello: 'world' }))
}
channel.onmessage = event => {
console.log('收到数据通道消息', event.data)
}
channel.onclose = () => {
console.log('关闭数据通道')
}
channel.onerror = () => {
console.log('数据通道发生错误')
}
return pc return pc
} }
// 向所有在线的用户广播消息(webrtc) // 向所有在线的用户广播消息(webrtc)
send_all(channel_name, data) { send_all(channel_name, data) {
console.log('向', channel_name, '频道广播消息:', data) console.log('向', channel_name, '频道广播消息:', data)
console.log('在线用户:', this.users)
this.users.filter(user => { this.users.filter(user => {
const status = user.webrtc.iceConnectionState const status = user.webrtc.iceConnectionState
return status === 'connected' || status === 'completed' return status === 'connected' || status === 'completed'
@ -153,7 +169,7 @@ export default class Entanglement {
set: (target, key, value) => { set: (target, key, value) => {
if (!Array.isArray(target) || key !== 'length') { if (!Array.isArray(target) || key !== 'length') {
console.log('对象被修改', [...path, key], value) console.log('对象被修改', [...path, key], value)
this.send_all(name, this.store[name]) // 向所有在线的用户广播消息 this.send_all(name, { key: [...path, key], value }) // 向所有在线的用户广播消息
} }
return Reflect.set(target, key, value) return Reflect.set(target, key, value)
}, },

View File

@ -13,13 +13,54 @@
</div> </div>
<script type="module"> <script type="module">
import Entanglement from './entanglement.js' import Entanglement from './entanglement.js'
const entanglement = new Entanglement({ options: {}, server: {} }) const entanglement = new Entanglement({
options: {
iceServers: [
{
urls: 'turn:satori.love:3478?transport=udp',
username: 'x-username',
credential: 'x-password'
},
{
urls: [
'stun:stun.1und1.de',
'stun:stun.callwithus.com',
'stun:stun.ekiga.net',
'stun:stun.fwdnet.net',
'stun:stun.fwdnet.net:3478',
'stun:stun.gmx.net',
'stun:stun.iptel.org',
'stun:stun.internetcalls.com',
'stun:stun.minisipserver.com',
'stun:stun.schlund.de',
'stun:stun.sipgate.net',
'stun:stun.sipgate.net:10000',
'stun:stun.softjoys.com',
'stun:stun.softjoys.com:3478',
'stun:stun.voip.aebc.com',
'stun:stun.voipbuster.com',
'stun:stun.voipstunt.com',
'stun:stun.voxgratia.org',
'stun:stun.wirlab.net',
'stun:stun.xten.com',
'stun:stunserver.org',
'stun:stun01.sipphone.com',
'stun:stun.zoiper.com'
]
}
],
iceCandidatePoolSize: 10, // 限制 ICE 候选者的数量
iceTransportPolicy: 'all', // 使用所有可用的候选者
bundlePolicy: 'balanced', // 每種類型的內容建立一個單獨的傳輸
}, server: {}
})
entanglement.set('json', { entanglement.set('json', {
users: { name: 'users', list: [{ name: 'satori' }] }, users: { name: 'users', list: [{ name: 'satori' }] },
music: { name: 'music', list: [{ name: 'satori' }] }, music: { name: 'music', list: [{ name: 'satori' }] },
image: { name: 'image', list: [{ name: 'satori' }] } image: { name: 'image', list: [{ name: 'satori' }] }
}) })
const store = entanglement.get('json') const store = entanglement.get('json')
await new Promise(resolve => setTimeout(resolve, 3000))
store.users.name = 'koishi' store.users.name = 'koishi'
//users.list.push({ name: 'koishi' }) //users.list.push({ name: 'koishi' })
</script> </script>