75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
import { io } from "socket.io-client";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
// FIXME: This file should handle the sockets and the subscriptions
|
|
// Exports must include
|
|
// - initSocket (initialize the connection to the socket server)
|
|
// - socket (variable resulting of initSocket function)
|
|
|
|
// Functions may include:
|
|
// - subscribe (subscribe to a room's stream or chat)
|
|
// - unsubscribe (unsubscribe from a room's stream or chat)
|
|
// - sendMessage (send a message to a room's chat)
|
|
|
|
let socket = null;
|
|
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");
|
|
|
|
const host = import.meta.env.VITE_HOST;
|
|
const port = import.meta.env.VITE_PORT;
|
|
const wsUri = "ws://" + host + (port ? ":" + port : "");
|
|
|
|
socket = io(wsUri, {
|
|
reconnectionDelayMax: 10000,
|
|
extraHeaders: {
|
|
Authorization: "Bearer " + token,
|
|
},
|
|
});
|
|
|
|
console.debug("Socket active: " + socket.active);
|
|
return;
|
|
}
|
|
|
|
// TODO
|
|
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() {
|
|
|
|
// }
|
|
// async function sendMessage() {
|
|
|
|
// }
|
|
|
|
export { initSocket, socket };
|