diff --git a/index.js b/index.js index dba0f1e..20609ab 100644 --- a/index.js +++ b/index.js @@ -81,7 +81,7 @@ app.ws('/entanglement', (ws, req) => { console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size) wsInstance.getWss().clients.forEach(client => { if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { - client.send(JSON.stringify({ type: 'pull', id: ws.id, channel: ws.channel })) + client.send(JSON.stringify({ type: 'pull', user: { id: ws.id, name: ws.name }, channel: ws.channel })) } }) }) @@ -90,7 +90,7 @@ app.ws('/entanglement', (ws, req) => { console.log(ws.id, '设备发生错误:', ws.channel, wsInstance.getWss().clients.size) wsInstance.getWss().clients.forEach(client => { if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { - client.send(JSON.stringify({ type: 'error', id: ws.id, channel: ws.channel })) + client.send(JSON.stringify({ type: 'error', user: { id: ws.id, name: ws.name }, channel: ws.channel })) } }) }) @@ -100,19 +100,21 @@ app.ws('/entanglement', (ws, req) => { const data = JSON.parse(message) wsInstance.getWss().clients.forEach(client => { if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === data.id) { - client.send(JSON.stringify({ ...data, id: ws.id, name: ws.name })) + client.send(JSON.stringify({ ...data, user: { id: ws.id, name: ws.name } })) } }) }) // 设备加入频道时广播给所有在线设备(也获取所有在线设备) console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size) + const list = [] wsInstance.getWss().clients.forEach(client => { if (client !== ws && client.readyState === 1 && client.channel === ws.channel) { console.log(ws.name, '广播给在线设备:', client.name) - client.send(JSON.stringify({ type: 'push', id: ws.id, name: ws.name, channel: ws.channel })) - ws.send(JSON.stringify({ type: 'list', id: client.id, name: client.name, channel: client.channel })) + client.send(JSON.stringify({ type: 'push', user: { id: ws.id, name: ws.name }, channel: ws.channel })) + list.push({ id: client.id, name: client.name }) } }) + ws.send(JSON.stringify({ type: 'list', list })) }) // WEBHOOK 处理 GitHub 事件 diff --git a/public/entanglement.js b/public/entanglement.js index 03368e4..8f3be09 100644 --- a/public/entanglement.js +++ b/public/entanglement.js @@ -18,7 +18,7 @@ export default class Entanglement { async __create_websocket() { const host = window.location.host const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws' - const ws = new WebSocket(`${protocol}://${host}/entanglement`) + const ws = new WebSocket(`${protocol}://${host}/entanglement?name=sato&channel=chat`) ws.onopen = () => console.log('websocket 连接成功') ws.onclose = async () => { console.log('websocket 连接关闭, 3s后尝试重新连接...') @@ -34,7 +34,7 @@ export default class Entanglement { const data = JSON.parse(event.data) //console.log('收到消息', data) if (data.type === 'list') { - console.debug('收到已在线对端列表', data.list) + console.debug('收到在线列表', data.list) this.users = Promise.all(data.list.map(async user => { console.debug('发送给', user.name, 'offer') const pc = this.__create_webrtc()