Add WebRTC TURN configuration and related enhancements

- Introduce a new `.env.example` file for environment variable setup, including `PUBLIC_IP`, `TURN_SECRET`, and `TURN_URLS` for WebRTC TURN server configuration.
- Update `docker-compose.yaml` to support TURN server deployment with necessary environment variables and commands.
- Enhance backend configuration and routes to include WebRTC ICE server settings, allowing for STUN/TURN server integration.
- Implement a new service for managing WebRTC ICE server configurations, providing credentials for TURN when configured.
- Modify frontend API to fetch ICE server configurations dynamically, improving support for cross-network voice preview.
This commit is contained in:
Xin Wang
2026-07-06 17:45:53 +08:00
parent d6b04d71a0
commit e857828fe5
9 changed files with 121 additions and 16 deletions

View File

@@ -19,7 +19,7 @@
import { useCallback, useEffect, useRef, useState } from "react";
import { API_BASE } from "@/lib/api";
import { API_BASE, webrtcApi } from "@/lib/api";
export type VoicePreviewStatus = "idle" | "connecting" | "connected" | "failed";
@@ -301,10 +301,12 @@ export function useVoicePreview(
if (wsRef.current === ws) closeOnRemoteEnd("语音信令连接已断开。");
};
// 2) 建 PeerConnection(STUN,本机/局域网够用)
const pc = new RTCPeerConnection({
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
});
// 2) 建 PeerConnection(STUN;公网跨网时后端会下发 TURN)
const iceServers = await webrtcApi
.iceServers()
.then((r) => r.iceServers)
.catch(() => [{ urls: "stun:stun.l.google.com:19302" }]);
const pc = new RTCPeerConnection({ iceServers });
pcRef.current = pc;
pc.onicecandidate = (e) => {

View File

@@ -208,3 +208,14 @@ export type NodeTypesResponse = {
export const nodeTypesApi = {
list: () => request<NodeTypesResponse>("/api/node-types"),
};
export type IceServerConfig = {
urls: string | string[];
username?: string;
credential?: string;
};
export const webrtcApi = {
iceServers: () =>
request<{ iceServers: IceServerConfig[] }>("/api/webrtc/ice-servers"),
};