This commit is contained in:
Guillem George 2026-05-15 22:59:15 +02:00
parent 441f00507d
commit a0b5e97058
3 changed files with 52 additions and 23 deletions

View file

@ -8,3 +8,19 @@
// - createRoom (create a room) // - createRoom (create a room)
// - updateRoom (update a room's configuration) // - updateRoom (update a room's configuration)
// - deleteRoom (delete a room) // - deleteRoom (delete a room)
//
// joinRoom notes
// io.on('connection', (socket) => {
// // join the room named 'some room'
// socket.join('some room');
// // broadcast to all connected clients in the room
// io.to('some room').emit('hello', 'world');
// // broadcast to all connected clients except those in the room
// io.except('some room').emit('hello', 'world');
// // leave the room
// socket.leave('some room');
// });

View file

@ -144,32 +144,26 @@ async function authedAPIRequest(endpoint, options) {
} }
options.headers.Authorization = "Bearer " + localStorage.getItem("token"); options.headers.Authorization = "Bearer " + localStorage.getItem("token");
options.mode = "cors";
const full_endpoint = import.meta.env.VITE_URL + "/api" + endpoint; const full_endpoint = import.meta.env.VITE_URL + "/api" + endpoint;
let response; let response;
try { response = await fetch(full_endpoint, options);
response = await fetch(full_endpoint, options); if (response.status === 401) {
if (!response.ok) { const response_err = await response.text();
throw new Error(response.statusText);
}
} catch (err) {
if (response.status === 401) {
const response_err = await response.text();
if (response_err.includes("Token expired")) { if (response_err.includes("Token expired")) {
await refreshToken(null); if (await refreshToken(null)) {
return null; return await authedAPIRequest(endpoint, options);
} }
localStorage.clear();
alert("Redirecting to logging page");
redirect.redirectToLoginPage();
return null; return null;
} }
console.error(err); localStorage.clear();
alert("Redirecting to logging page");
redirect.redirectToLoginPage();
return null;
} }
return response; return response;

View file

@ -1,5 +1,5 @@
import { io } from "socket.io-client"; import { io } from "socket.io-client";
// import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
// FIXME: This file should handle the sockets and the subscriptions // FIXME: This file should handle the sockets and the subscriptions
// Exports must include // Exports must include
@ -12,13 +12,18 @@ import { io } from "socket.io-client";
// - sendMessage (send a message to a room's chat) // - sendMessage (send a message to a room's chat)
let socket = null; let socket = null;
// let uuid = uuidv4(); const uuid = uuidv4();
/** /**
* Initializes the socket when authenticated * Initializes the socket when authenticated
* returns {Promise<void>} * returns {Promise<void>}
*/ */
async function initSocket() { async function initSocket() {
if (socket !== null) {
console.warn("Blocked attempt to re-init socket connection");
return;
}
console.debug("Initializing socket connection"); console.debug("Initializing socket connection");
const token = localStorage.getItem("token"); const token = localStorage.getItem("token");
@ -38,13 +43,27 @@ async function initSocket() {
} }
// TODO // TODO
// async function subscribe(room) { async function subscribe(room) {
if (!room) {
room = "epi-place";
}
// if (!room) // console.warn("Skipping room susbscription (not implemented)")
// room = "epi-place" const msg = {
id: uuid,
method: "subscription",
params: {
path: "rooms.canvas.getStream" | "rooms.getChat",
input: {
json: {
roomSlug: room,
},
},
},
};
// console.warn("Skipping room susbscription (not implemented)") socket.send(msg);
// } }
// async function unsubscribe() { // async function unsubscribe() {