日志分级

This commit is contained in:
2023-10-05 04:43:57 +08:00
parent a8603e16a3
commit cec55b72f9
3 changed files with 23 additions and 13 deletions

View File

@ -1,9 +1,14 @@
# webRTC # webRTC
webrtc 实现的 p2p 信道 webrtc 实现的 p2p 信道
rtc rtc rtc
channel channel channel channel
part-server
- [x] P2P通信 - [x] P2P通信
- [ ] 分离出主要功能, 作为库或桁架使用 - [ ] 分离出主要功能, 作为库或桁架使用
- [ ] 静态资源服务模式 - [ ] 静态资源服务模式(音乐,图像,视频,文本,各种,即时聊天)
- [ ] 集群分发 - [ ] 集群分发
- [x] 音乐播放 - [x] 音乐播放
- [x] 请求到单个目标防止接收到重复分片数据 - [x] 请求到单个目标防止接收到重复分片数据

View File

@ -59,11 +59,11 @@ export default class ClientList {
bundlePolicy: 'balanced', // 每種類型的內容建立一個單獨的傳輸 bundlePolicy: 'balanced', // 每種類型的內容建立一個單獨的傳輸
}) })
webrtc.ondatachannel = ({ channel }) => { 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 client = this.clientlist.find(x => x.id === data.id)
const option = this.channels[channel.label] const option = this.channels[channel.label]
channel.onopen = event => { channel.onopen = event => {
console.log('对方打开', channel.label, '数据通道') console.debug('对方打开', channel.label, '数据通道')
if (option && option.onopen) { if (option && option.onopen) {
option.onopen(event, client) option.onopen(event, client)
} }
@ -75,13 +75,13 @@ export default class ClientList {
} }
} }
channel.onclose = event => { channel.onclose = event => {
console.log('对方关闭', channel.label, '数据通道') console.debug('对方关闭', channel.label, '数据通道')
if (option && option.onclose) { if (option && option.onclose) {
option.onclose(event, client) option.onclose(event, client)
} }
} }
channel.onerror = event => { channel.onerror = event => {
console.log(data.name, '通道', channel.label, '发生错误') console.error(data.name, '通道', channel.label, '发生错误')
if (option && option.onerror) { if (option && option.onerror) {
option.onerror(event, client) option.onerror(event, client)
} }
@ -100,10 +100,10 @@ export default class ClientList {
webrtc.getTransceivers webrtc.getTransceivers
webrtc.oniceconnectionstatechange = async event => { webrtc.oniceconnectionstatechange = async event => {
if (webrtc.iceConnectionState === 'disconnected' || webrtc.iceConnectionState === 'failed') { if (webrtc.iceConnectionState === 'disconnected' || webrtc.iceConnectionState === 'failed') {
console.log(data.name, '需要添加新的 candidate') console.error(data.name, '需要添加新的 candidate')
// 添加新的 candidate // 添加新的 candidate
} else if (webrtc.iceConnectionState === 'connected' || webrtc.iceConnectionState === 'completed') { } 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]) => { const channels = Object.entries(this.channels).map(([name, callback]) => {
@ -302,7 +302,11 @@ export default class ClientList {
sendto(id, name, data) { sendto(id, name, data) {
const client = this.clientlist.find(client => client.id === id) const client = this.clientlist.find(client => client.id === id)
if (!client) { if (!client) {
console.log('客户端不存在:', id) console.error('客户端不存在:', id)
return
}
if (!client.channels.find(ch => ch.label === name)) {
console.error('通道不存在:', name)
return return
} }
client.channels.filter(ch => ch.label === name).forEach(async ch => { client.channels.filter(ch => ch.label === name).forEach(async ch => {

View File

@ -156,11 +156,12 @@
const data = JSON.parse(event.data) const data = JSON.parse(event.data)
if (data.type === 'get_user_profile') { if (data.type === 'get_user_profile') {
console.log(client.name, '请求身份信息:', data) console.log(client.name, '请求身份信息:', data)
//clientList.sendto(client.id, 'base', JSON.stringify({ // 包过大会导致发送失败, 因此需要分开发送
// type: 'set_user_profile', clientList.sendto(client.id, 'base', JSON.stringify({
// name: name, type: 'set_user_profile',
// avatar: avatar, name: name,
//})) avatar: avatar,
}))
return return
} }
if (data.type === 'set_user_profile') { if (data.type === 'set_user_profile') {