Skip to content

Commit

Permalink
add cloudflare turn server
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 14, 2024
1 parent 8dc2387 commit e9dedd6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/chat-api/services/VoiceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
5 changes: 3 additions & 2 deletions src/chat-api/store/useChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});
Expand Down
7 changes: 7 additions & 0 deletions src/chat-api/store/useVoiceUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"],
},
Expand Down Expand Up @@ -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"],
},
Expand Down

0 comments on commit e9dedd6

Please sign in to comment.