wip voice chat

This commit is contained in:
pb-coding 2023-10-11 13:38:16 +02:00
parent 62d801becb
commit c9d8959d21
2 changed files with 13 additions and 1 deletions

View file

@ -27,9 +27,16 @@ export const handleJoinSession = (
callback && callback("success" satisfies SessionResponse); callback && callback("success" satisfies SessionResponse);
console.log("User joined session:", sessionId); console.log("User joined session:", sessionId);
const numberOfClients = io.sockets.adapter.rooms.get(sessionId)?.size ?? 0; const clientsInRoom = io.sockets.adapter.rooms.get(sessionId) || new Set();
const numberOfClients = clientsInRoom.size ?? 0;
console.log("Clients in room:", numberOfClients); console.log("Clients in room:", numberOfClients);
io.to(sessionId).emit("clients-in-session", numberOfClients); io.to(sessionId).emit("clients-in-session", numberOfClients);
clientsInRoom.forEach((clientId) => {
if (clientId !== socket.id) {
io.to(clientId).emit("initiate-voice-chat", { to: socket.id });
}
});
}; };
export const handleLeaveSession = (socket: Socket, sessionId: string) => { export const handleLeaveSession = (socket: Socket, sessionId: string) => {

View file

@ -47,6 +47,11 @@ io.on("connection", (socket: Socket) => {
handleLeaveSession(socket, sessionId); handleLeaveSession(socket, sessionId);
}); });
socket.on("signal", (data: any) => {
const { to, signalData } = data;
io.to(to).emit("signal", { from: socket.id, signalData });
});
socket.on("new-game", (gameDetails: { sessionId: string }) => socket.on("new-game", (gameDetails: { sessionId: string }) =>
// TODO: get sessionId from socket instead of passing it from client // TODO: get sessionId from socket instead of passing it from client
handleNewGame(socket, gameDetails) handleNewGame(socket, gameDetails)