webrtc/index.js

129 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2023-09-27 14:26:12 +08:00
import express from 'express'
import expressWs from 'express-ws'
2023-10-01 12:47:39 +08:00
import turn from 'node-turn'
2023-09-28 16:06:38 +08:00
import { exec } from 'child_process'
2023-09-27 14:26:12 +08:00
2023-10-01 12:47:39 +08:00
// 创建 TURN 服务器
const turnServer = new turn({
2023-10-21 08:38:06 +08:00
debugLevel: 'WARN', // ALL, DEBUG, INFO, WARN, ERROR, NONE
2023-10-01 12:47:39 +08:00
})
// 启动 TURN 服务器
turnServer.start(() => {
console.log('TURN server start:', turnServer)
})
2023-09-27 14:26:12 +08:00
const app = express()
const wsInstance = expressWs(app)
2023-10-13 05:51:45 +08:00
app.use(express.static('dist'))
2023-10-01 12:47:39 +08:00
app.use(express.json())
app.use((req, res, next) => {
if (req.method === 'CONNECT') {
turnServer.handleConnect(req, res)
} else {
next()
}
})
2023-09-27 14:26:12 +08:00
// Websocket 处理 webRTC 信令
app.ws('/webrtc/:channel', (ws, req) => {
2023-10-06 13:52:09 +08:00
ws.id = req.headers['sec-websocket-key']
ws.channel = req.params.channel
ws.name = req.query.name
console.log('ws.name:', ws.name)
// 设备离开频道时广播给所有在线设备
ws.on('close', () => {
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 }))
}
})
})
// 设备发生错误时广播给所有在线设备
ws.on('error', () => {
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 }))
}
})
})
// 设备发送信令时转发给指定在线设备
ws.on('message', message => {
console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size)
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 }))
}
})
})
// 设备加入频道时广播给所有在线设备(也获取所有在线设备)
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) {
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 }))
}
})
})
// Entanglement
app.ws('/entanglement', (ws, req) => {
2023-09-27 14:26:12 +08:00
ws.id = req.headers['sec-websocket-key']
ws.channel = req.params.channel
2023-09-30 22:45:01 +08:00
ws.name = req.query.name
2023-10-01 01:22:39 +08:00
console.log('ws.name:', ws.name)
2023-09-27 14:26:12 +08:00
// 设备离开频道时广播给所有在线设备
ws.on('close', () => {
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) {
2023-10-06 14:27:58 +08:00
client.send(JSON.stringify({ type: 'pull', user: { id: ws.id, name: ws.name }, channel: ws.channel }))
2023-09-27 14:26:12 +08:00
}
})
})
// 设备发生错误时广播给所有在线设备
ws.on('error', () => {
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) {
2023-10-06 14:27:58 +08:00
client.send(JSON.stringify({ type: 'error', user: { id: ws.id, name: ws.name }, channel: ws.channel }))
2023-09-27 14:26:12 +08:00
}
})
})
// 设备发送信令时转发给指定在线设备
ws.on('message', message => {
console.log(ws.id, '设备发送信令:', ws.channel, wsInstance.getWss().clients.size)
2023-09-28 23:49:26 +08:00
const data = JSON.parse(message)
2023-10-06 15:09:52 +08:00
console.log('icecandidate:', data)
2023-09-27 14:26:12 +08:00
wsInstance.getWss().clients.forEach(client => {
2023-10-06 15:09:52 +08:00
if (client !== ws && client.readyState === 1 && client.channel === ws.channel && client.id === data.user.id) {
2023-10-06 14:27:58 +08:00
client.send(JSON.stringify({ ...data, user: { id: ws.id, name: ws.name } }))
2023-09-27 14:26:12 +08:00
}
})
})
// 设备加入频道时广播给所有在线设备(也获取所有在线设备)
console.log(ws.id, '设备加入频道:', ws.channel, wsInstance.getWss().clients.size)
2023-10-06 14:27:58 +08:00
const list = []
2023-09-27 14:26:12 +08:00
wsInstance.getWss().clients.forEach(client => {
if (client !== ws && client.readyState === 1 && client.channel === ws.channel) {
2023-10-01 01:22:39 +08:00
console.log(ws.name, '广播给在线设备:', client.name)
2023-10-06 14:27:58 +08:00
client.send(JSON.stringify({ type: 'push', user: { id: ws.id, name: ws.name }, channel: ws.channel }))
list.push({ id: client.id, name: client.name })
2023-09-27 14:26:12 +08:00
}
})
2023-10-06 14:27:58 +08:00
ws.send(JSON.stringify({ type: 'list', list }))
2023-09-27 14:26:12 +08:00
})
2023-09-28 16:06:38 +08:00
// WEBHOOK 处理 GitHub 事件
app.post('/webhook', (req, res) => {
console.log('WEBHOOK:' + new Date().toLocaleString())
2023-12-11 17:32:02 +08:00
exec('git pull;npm i;npm run build;pm2 reload webrtc;')
return res.json({ success: true })
2023-09-28 16:06:38 +08:00
})
2023-09-28 16:03:19 +08:00
app.listen(4096, () => console.log('Server started on port 4096'))