From e9dedd60cdd98234e24a286cec7f6a7214d43c30 Mon Sep 17 00:00:00 2001 From: Supertiger Date: Mon, 14 Oct 2024 10:23:10 +0100 Subject: [PATCH] add cloudflare turn server --- src/chat-api/services/VoiceService.ts | 25 +++++++++++++++++++++++++ src/chat-api/store/useChannels.ts | 5 +++-- src/chat-api/store/useVoiceUsers.ts | 7 +++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/chat-api/services/VoiceService.ts b/src/chat-api/services/VoiceService.ts index 36d1b424..76f3ecfa 100644 --- a/src/chat-api/services/VoiceService.ts +++ b/src/chat-api/services/VoiceService.ts @@ -22,3 +22,28 @@ export const postLeaveVoice = async (channelId: string) => { }); return data; }; + + +const lastCredentials = { + generatedAt: null as null | number, + result: null as null | any +}; +export const postGenerateCredential = async () => { + if (lastCredentials.generatedAt) { + const diff = Date.now() - lastCredentials.generatedAt; + // 1 hour after last generated + if (diff < 60 * 60 * 1000) { + return lastCredentials as { result: any }; + } + } + const data = await request<{ result: any }>({ + method: "POST", + url: env.SERVER_URL + "/api/voice/generate", + useToken: true + }); + + lastCredentials.generatedAt = Date.now(); + lastCredentials.result = data.result; + + return data; +}; \ No newline at end of file diff --git a/src/chat-api/store/useChannels.ts b/src/chat-api/store/useChannels.ts index 74620389..15fe38a4 100644 --- a/src/chat-api/store/useChannels.ts +++ b/src/chat-api/store/useChannels.ts @@ -17,7 +17,7 @@ import useServerMembers from "./useServerMembers"; import useAccount from "./useAccount"; import useMention from "./useMention"; import socketClient from "../socketClient"; -import { postJoinVoice, postLeaveVoice } from "../services/VoiceService"; +import { postGenerateCredential, postJoinVoice, postLeaveVoice } from "../services/VoiceService"; import useVoiceUsers from "./useVoiceUsers"; import { useMatch, useNavigate, useParams } from "solid-navigator"; import RouterEndpoints from "@/common/RouterEndpoints"; @@ -109,8 +109,9 @@ function recipient(this: Channel) { return users.get(this.recipientId!); } -function joinCall(this: Channel) { +async function joinCall(this: Channel) { const { setCurrentVoiceChannelId } = useVoiceUsers(); + await postGenerateCredential(); postJoinVoice(this.id, socketClient.id()!).then(() => { setCurrentVoiceChannelId(this.id); }); diff --git a/src/chat-api/store/useVoiceUsers.ts b/src/chat-api/store/useVoiceUsers.ts index cc287a2e..c9e8f57d 100644 --- a/src/chat-api/store/useVoiceUsers.ts +++ b/src/chat-api/store/useVoiceUsers.ts @@ -9,6 +9,7 @@ import useChannels from "./useChannels"; import env from "@/common/env"; import vad from "voice-activity-detection"; import { getStorageString, StorageKeys } from "@/common/localStorage"; +import { postGenerateCredential } from "../services/VoiceService"; interface VADInstance { connect: () => void; @@ -90,12 +91,15 @@ async function addPeer(this: VoiceUser, signal: SimplePeer.SignalData) { console.log(user.username, "peer added"); const { default: LazySimplePeer } = await import("@thaunknown/simple-peer"); + const turnServer = await postGenerateCredential(); + const peer = new LazySimplePeer({ initiator: false, trickle: true, config: { iceServers: [ + turnServer.result, { urls: ["stun:stun.l.google.com:19302"], }, @@ -190,12 +194,15 @@ export async function createPeer(voiceUser: VoiceUser | RawVoice) { console.log(user.username, "peer created"); const { default: LazySimplePeer } = await import("@thaunknown/simple-peer"); + const turnServer = await postGenerateCredential(); + const peer = new LazySimplePeer({ initiator: true, trickle: true, config: { iceServers: [ + turnServer.result, { urls: ["stun:stun.l.google.com:19302"], },