diff --git a/src/rooms/index.js b/src/rooms/index.js index 42b9eca..f70fe6e 100644 --- a/src/rooms/index.js +++ b/src/rooms/index.js @@ -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'); +// }); diff --git a/src/utils/auth.js b/src/utils/auth.js index 3dfa666..015db2a 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -144,32 +144,26 @@ 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(); + response = await fetch(full_endpoint, options); + if (response.status === 401) { + const response_err = await response.text(); - if (response_err.includes("Token expired")) { - await refreshToken(null); - return null; + if (response_err.includes("Token expired")) { + if (await refreshToken(null)) { + return await authedAPIRequest(endpoint, options); } - localStorage.clear(); - alert("Redirecting to logging page"); - redirect.redirectToLoginPage(); return null; } - console.error(err); + localStorage.clear(); + alert("Redirecting to logging page"); + redirect.redirectToLoginPage(); + return null; } return response; diff --git a/src/utils/streams.js b/src/utils/streams.js index 8b01319..938f0dd 100644 --- a/src/utils/streams.js +++ b/src/utils/streams.js @@ -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} */ 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) { +async function subscribe(room) { + if (!room) { + room = "epi-place"; + } -// 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, + }, + }, + }, + }; -// console.warn("Skipping room susbscription (not implemented)") -// } + socket.send(msg); +} // async function unsubscribe() {