entanglement server
This commit is contained in:
parent
9340c99755
commit
7d62522f7e
12
index.js
12
index.js
@ -81,7 +81,7 @@ app.ws('/entanglement', (ws, req) => {
|
|||||||
console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size)
|
console.log(ws.id, '设备离开频道:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
wsInstance.getWss().clients.forEach(client => {
|
||||||
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
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)
|
console.log(ws.id, '设备发生错误:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
wsInstance.getWss().clients.forEach(client => {
|
||||||
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
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)
|
const data = JSON.parse(message)
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
wsInstance.getWss().clients.forEach(client => {
|
||||||
if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === data.id) {
|
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)
|
console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size)
|
||||||
|
const list = []
|
||||||
wsInstance.getWss().clients.forEach(client => {
|
wsInstance.getWss().clients.forEach(client => {
|
||||||
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
|
||||||
console.log(ws.name, '广播给在线设备:', client.name)
|
console.log(ws.name, '广播给在线设备:', client.name)
|
||||||
client.send(JSON.stringify({ type: 'push', id: ws.id, name: ws.name, channel: ws.channel }))
|
client.send(JSON.stringify({ type: 'push', user: { id: ws.id, name: ws.name }, channel: ws.channel }))
|
||||||
ws.send(JSON.stringify({ type: 'list', id: client.id, name: client.name, channel: client.channel }))
|
list.push({ id: client.id, name: client.name })
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
ws.send(JSON.stringify({ type: 'list', list }))
|
||||||
})
|
})
|
||||||
|
|
||||||
// WEBHOOK 处理 GitHub 事件
|
// WEBHOOK 处理 GitHub 事件
|
||||||
|
@ -18,7 +18,7 @@ export default class Entanglement {
|
|||||||
async __create_websocket() {
|
async __create_websocket() {
|
||||||
const host = window.location.host
|
const host = window.location.host
|
||||||
const protocol = window.location.protocol === 'https:' ? 'wss' : 'ws'
|
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.onopen = () => console.log('websocket 连接成功')
|
||||||
ws.onclose = async () => {
|
ws.onclose = async () => {
|
||||||
console.log('websocket 连接关闭, 3s后尝试重新连接...')
|
console.log('websocket 连接关闭, 3s后尝试重新连接...')
|
||||||
@ -34,7 +34,7 @@ export default class Entanglement {
|
|||||||
const data = JSON.parse(event.data)
|
const data = JSON.parse(event.data)
|
||||||
//console.log('收到消息', data)
|
//console.log('收到消息', data)
|
||||||
if (data.type === 'list') {
|
if (data.type === 'list') {
|
||||||
console.debug('收到已在线对端列表', data.list)
|
console.debug('收到在线列表', data.list)
|
||||||
this.users = Promise.all(data.list.map(async user => {
|
this.users = Promise.all(data.list.map(async user => {
|
||||||
console.debug('发送给', user.name, 'offer')
|
console.debug('发送给', user.name, 'offer')
|
||||||
const pc = this.__create_webrtc()
|
const pc = this.__create_webrtc()
|
||||||
|
Loading…
Reference in New Issue
Block a user