diff --git a/index.js b/index.js index fb2429b..113f05f 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,39 @@ import express from 'express' import expressWs from 'express-ws' +import turn from 'node-turn' import { exec } from 'child_process' +// 创建 TURN 服务器 +const turnServer = new turn({ + authMech: 'long-term', + credentials: { + username: 'your-username', + password: 'your-password', + }, + debugLevel: 'ALL', + listeningIps: [''], + listeningPort: 3478, + relayIps: [''], + relayPort: 3478, + verbose: true +}) + +// 启动 TURN 服务器 +turnServer.start(() => { + console.log('TURN server start:', turnServer) +}) + const app = express() const wsInstance = expressWs(app) app.use(express.static('public')) +app.use(express.json()) +app.use((req, res, next) => { + if (req.method === 'CONNECT') { + turnServer.handleConnect(req, res) + } else { + next() + } +}) // Websocket 处理 webRTC 信令 app.ws('/webrtc/:channel', (ws, req) => { diff --git a/package.json b/package.json index 236db8e..8226b73 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "dependencies": { "express": "^4.18.2", - "express-ws": "^5.0.2" + "express-ws": "^5.0.2", + "node-turn": "^0.0.6" } } diff --git a/public/client.js b/public/client.js index 29b50b0..e9ac147 100644 --- a/public/client.js +++ b/public/client.js @@ -16,7 +16,13 @@ export default class ClientList { websocket.onmessage = async event => { const data = JSON.parse(event.data) const webrtc_init = () => { - const webrtc = new RTCPeerConnection() + const webrtc = new RTCPeerConnection({ + iceServers: [{ + urls: 'turn:satori.love:3478', + username: 'your-username', + credential: 'your-password' + }] + }) webrtc.onicecandidate = event => { if (event.candidate) { websocket.send(JSON.stringify({