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)
// - 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');
// // 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,22 +144,19 @@ async function authedAPIRequest(endpoint, options) {
}
options.headers.Authorization = "Bearer " + localStorage.getItem("token");
options.mode = "cors";
const full_endpoint = import.meta.env.VITE_URL + "/api" + endpoint;
let response;
try {
response = await fetch(full_endpoint, options);
if (!response.ok) {
throw new Error(response.statusText);
}
} catch (err) {
if (response.status === 401) {
const response_err = await response.text();
if (response_err.includes("Token expired")) {
await refreshToken(null);
if (await refreshToken(null)) {
return await authedAPIRequest(endpoint, options);
}
return null;
}
@ -169,9 +166,6 @@ async function authedAPIRequest(endpoint, options) {
return null;
}
console.error(err);
}
return response;
}

View file

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