TURN 服务器

This commit is contained in:
2023-10-01 12:47:39 +08:00
parent 2ce424dc6b
commit 914ae000d5
3 changed files with 38 additions and 2 deletions

View File

@ -1,10 +1,39 @@
import express from 'express' import express from 'express'
import expressWs from 'express-ws' import expressWs from 'express-ws'
import turn from 'node-turn'
import { exec } from 'child_process' 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 app = express()
const wsInstance = expressWs(app) const wsInstance = expressWs(app)
app.use(express.static('public')) 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 信令 // Websocket 处理 webRTC 信令
app.ws('/webrtc/:channel', (ws, req) => { app.ws('/webrtc/:channel', (ws, req) => {

View File

@ -15,6 +15,7 @@
}, },
"dependencies": { "dependencies": {
"express": "^4.18.2", "express": "^4.18.2",
"express-ws": "^5.0.2" "express-ws": "^5.0.2",
"node-turn": "^0.0.6"
} }
} }

View File

@ -16,7 +16,13 @@ export default class ClientList {
websocket.onmessage = async event => { websocket.onmessage = async event => {
const data = JSON.parse(event.data) const data = JSON.parse(event.data)
const webrtc_init = () => { 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 => { webrtc.onicecandidate = event => {
if (event.candidate) { if (event.candidate) {
websocket.send(JSON.stringify({ websocket.send(JSON.stringify({