Join voice channel with sockets

This commit is contained in:
Kevin Puig
2025-09-28 05:39:05 -04:00
parent 69b03ecc61
commit 226ee3f998
3 changed files with 130 additions and 24 deletions

View File

@@ -1,33 +1,70 @@
import { Server, Socket } from "socket.io";
import { getUserCredentials, getUserInformation } from "../services/userService";
import { getAllInstances, getInstanceByChannelId, getInstancesByUserId } from "../services/instanceService";
import { getCategoriesByInstance, getCategory, getChannel } from "../services/channelService";
// Change to Map of voiceChannelId to Map of userId to socket
const voiceChannelMembers = new Map<string, Map<string, Socket>>();
//TEST IGNORE
export function registerVoiceHandlers(io: Server) {
io.on("connection", (socket: Socket) => {
console.log(`Voice socket connected: ${socket.id}`);
// Join voice channel
socket.on("join-voicechannel", async (data) => {
const payload = data as {
userId: string
userToken: string,
voiceChannelId: string,
};
if (!payload) {
socket.emit("error-voicechannel", "no payload in voice conn")
return;
}
socket.on("join-voice-channel", (channelId: string) => {
socket.join(channelId);
console.log(`Socket ${socket.id} joined voice channel ${channelId}`);
// Optionally, notify others in the channel
socket.to(channelId).emit("user-joined-voice", socket.id);
// Initialize map for channel if not present
if (!voiceChannelMembers.has(payload.voiceChannelId)) {
voiceChannelMembers.set(payload.voiceChannelId, new Map());
}
const channelMembers = voiceChannelMembers.get(payload.voiceChannelId)!;
// Remove user if already present in this channel
if (channelMembers.has(payload.userId)) {
channelMembers.delete(payload.userId);
}
// authenticate user
const userCreds = await getUserCredentials(payload.userId);
if (!userCreds || !userCreds.token || userCreds.token != payload.userToken) {
socket.emit("error-voicechannel", "bad user creds in voice conn");
return;
}
// determine if channel is voice channel
const channel = await getChannel(payload.voiceChannelId);
if (!channel || channel.type !== "voice" || !channel.categoryId) {
socket.emit("error-voicechannel", "bad channel or channel type in voice conn");
return;
}
// authorize user using role
const user = await getUserInformation(payload.userId);
const instance = await getInstanceByChannelId(payload.voiceChannelId);
const instances = await getInstancesByUserId(payload.userId);
if (!user || !instance || !instances || !instances.find(e => e.id === instance.id)) {
socket.emit("error-voicechannel", "user not authorized for channel in voice conn");
return;
}
// add to map
channelMembers.set(payload.userId, socket);
socket.join(payload.voiceChannelId);
socket.emit("joined-voicechannel", {
voiceChannelId: payload.voiceChannelId,
connectedUserIds: Array.from(channelMembers.keys()).filter(e => e !== payload.userId)
});
socket.to(payload.voiceChannelId).emit("user-joined-voicechannel", { userId: payload.userId });
});
socket.on("leave-voice-channel", (channelId: string) => {
socket.leave(channelId);
console.log(`Socket ${socket.id} left voice channel ${channelId}`);
// Optionally, notify others in the channel
socket.to(channelId).emit("user-left-voice", socket.id);
});
socket.on("voice-data", (channelId: string, data: any) => {
// Broadcast voice data to all other clients in the same channel
socket.to(channelId).emit("voice-data", socket.id, data);
});
socket.on("disconnect", () => {
console.log(`Voice socket disconnected: ${socket.id}`);
// Handle user leaving all voice channels they were in
// (e.g., iterate through socket.rooms if you need to emit to specific channels)
});
});
}