join the channel of the room you want to subscribe on the server you just initialized a socket on

This commit is contained in:
Guillem George 2026-05-16 12:18:35 +02:00
parent 2b7b6b8684
commit 02ac670fe1
4 changed files with 81 additions and 35 deletions

View file

@ -1,4 +1,5 @@
import { authedAPIRequest } from "../utils/auth";
import { socket } from "../pages/index";
// FIXME: This file should handle the rooms API
// Functions may include:
// - fetchRoomConfig (get the configuration of a room)
@ -9,26 +10,55 @@ import { authedAPIRequest } from "../utils/auth";
// - createRoom (create a room)
// - updateRoom (update a room's configuration)
// - deleteRoom (delete a room)
//
// joinRoom notes
// io.on('connection', (socket) => {
// // join the room named 'some room'
// socket.join('some room');
const roomsConfig = null;
// // broadcast to all connected clients in the room
// io.to('some room').emit('hello', 'world');
function setCurrentRoomConfig(room, cfg) {
roomsConfig[room] = cfg;
}
function getCurrentRoomConfig(room) {
return roomsConfig[room];
}
async function joinRoom(room) {
socket.on("connection", (sockett) => {
sockett.join(room);
// // broadcast to all connected clients except those in the room
// io.except('some room').emit('hello', 'world');
// broadcast to all connected clients in the room
// socket.to('some room').emit('hello', 'world');
// // leave the room
// socket.leave('some room');
// });
//
// broadcast to all connected clients except those in the room
// socket.except('some room').emit('hello', 'world');
});
}
// async function leaveRoom(room) {
// io.on('connection', (socket) => {
// socket.leave(room);
// })
// }
// async function listRooms() {
// }
// async function createRoom() {
// }
// async function updateRoom() {
// }
// async function deleteRoom() {
// }
async function fetchRoomConfig(room) {
const response = await authedAPIRequest("/rooms/" + room + "/config");
if (!room) {
console.error("Cannot fetch an undefined room");
return;
}
const response = await authedAPIRequest("/rooms/" + room + "/config", {
method: "GET",
});
if (!response.ok) {
console.error("Could not retrieve room config: " + response.statusText);
@ -36,11 +66,26 @@ async function fetchRoomConfig(room) {
return;
}
const obj = await response.json();
const res = await response.json();
console.debug(`Retrieved config for room ${room}: ${obj}`);
console.debug(`Retrieved config for room ${room}:`);
console.debug(res);
setCurrentRoomConfig(res);
// Update HTML
const roomNameElt = document.getElementById("room-name");
roomNameElt.innerText = res.metadata.name;
}
export { fetchRoomConfig };
export {
fetchRoomConfig,
setCurrentRoomConfig,
getCurrentRoomConfig,
joinRoom,
// listRooms,
// createRoom,
// updateRoom,
// deleteRoom
};