diff --git a/README.md b/README.md index f7e01fc..5bf3d68 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,14 @@ # webRTC webrtc 实现的 p2p 信道 +rtc rtc rtc +channel channel channel channel +part-server + + - [x] P2P通信 - [ ] 分离出主要功能, 作为库或桁架使用 - - [ ] 静态资源服务模式 + - [ ] 静态资源服务模式(音乐,图像,视频,文本,各种,即时聊天) - [ ] 集群分发 - [x] 音乐播放 - [x] 请求到单个目标防止接收到重复分片数据 diff --git a/public/client.js b/public/client.js index e8d3221..13376bb 100644 --- a/public/client.js +++ b/public/client.js @@ -59,11 +59,11 @@ export default class ClientList { bundlePolicy: 'balanced', // 每種類型的內容建立一個單獨的傳輸 }) webrtc.ondatachannel = ({ channel }) => { - console.log(data.name, '建立', channel.label, '数据通道') + console.debug(data.name, '建立', channel.label, '数据通道') const client = this.clientlist.find(x => x.id === data.id) const option = this.channels[channel.label] channel.onopen = event => { - console.log('对方打开', channel.label, '数据通道') + console.debug('对方打开', channel.label, '数据通道') if (option && option.onopen) { option.onopen(event, client) } @@ -75,13 +75,13 @@ export default class ClientList { } } channel.onclose = event => { - console.log('对方关闭', channel.label, '数据通道') + console.debug('对方关闭', channel.label, '数据通道') if (option && option.onclose) { option.onclose(event, client) } } channel.onerror = event => { - console.log(data.name, '通道', channel.label, '发生错误') + console.error(data.name, '通道', channel.label, '发生错误') if (option && option.onerror) { option.onerror(event, client) } @@ -100,10 +100,10 @@ export default class ClientList { webrtc.getTransceivers webrtc.oniceconnectionstatechange = async event => { if (webrtc.iceConnectionState === 'disconnected' || webrtc.iceConnectionState === 'failed') { - console.log(data.name, '需要添加新的 candidate') + console.error(data.name, '需要添加新的 candidate') // 添加新的 candidate } else if (webrtc.iceConnectionState === 'connected' || webrtc.iceConnectionState === 'completed') { - console.log(data.name, 'WebRTC 连接已经建立成功') + console.debug(data.name, 'WebRTC 连接已经建立成功') } } const channels = Object.entries(this.channels).map(([name, callback]) => { @@ -302,7 +302,11 @@ export default class ClientList { sendto(id, name, data) { const client = this.clientlist.find(client => client.id === id) if (!client) { - console.log('客户端不存在:', id) + console.error('客户端不存在:', id) + return + } + if (!client.channels.find(ch => ch.label === name)) { + console.error('通道不存在:', name) return } client.channels.filter(ch => ch.label === name).forEach(async ch => { diff --git a/public/index.html b/public/index.html index 070aa90..5be072e 100644 --- a/public/index.html +++ b/public/index.html @@ -156,11 +156,12 @@ const data = JSON.parse(event.data) if (data.type === 'get_user_profile') { console.log(client.name, '请求身份信息:', data) - //clientList.sendto(client.id, 'base', JSON.stringify({ - // type: 'set_user_profile', - // name: name, - // avatar: avatar, - //})) + // 包过大会导致发送失败, 因此需要分开发送 + clientList.sendto(client.id, 'base', JSON.stringify({ + type: 'set_user_profile', + name: name, + avatar: avatar, + })) return } if (data.type === 'set_user_profile') {