Files
DRB-Client/modules/socketClient.mjs
2024-05-12 12:55:00 -04:00

52 lines
2.0 KiB
JavaScript

import { io } from "socket.io-client";
import { logIntoServerWrapper, nodeCheckStatus, nodeJoinServer, nodeLeaveServer, nodeGetUsername, nodeCheckDiscordClientStatus, nodeCheckCurrentSystem, nodeUpdate, nodeGetDiscordID } from "./socketClientWrappers.mjs";
/**
* Initialize the socket connection with the server, this will handle disconnects within itself
* @param {Object} localNodeConfig The local node config object
* @returns {any}
*/
export const initSocketConnection = async (localNodeConfig) => {
const serverEndpoint = `http://${localNodeConfig.serverIp}:${localNodeConfig.serverPort}` || 'http://localhost:3000'; // Adjust the server endpoint
const socket = io.connect(serverEndpoint);
// Socket Events ('system' events persay)
// When the socket connects to the node server
socket.on('connect', async () => {
console.log('Connected to the server');
await logIntoServerWrapper(socket, localNodeConfig);
});
// When the socket disconnects from the node server
socket.on('disconnect', () => {
console.log('Disconnected from the server');
});
// Node events/commands
// Requested the node update itself
socket.on('node-update', nodeUpdate);
// Requested to join a discord guild and listen to a system
socket.on('node-join', nodeJoinServer);
// Requested to leave a discord guild
socket.on('node-leave', nodeLeaveServer);
// Requested to get the discord username in a given guild
socket.on('node-get-discord-username', nodeGetUsername);
// Requested to get the ID of the active discord client
socket.on('node-get-discord-id', nodeGetDiscordID);
// Requested to check if the node is connected to VC in a given guild
socket.on('node-check-connected-status', nodeCheckStatus);
// Requested to check if the node has an open discord client
socket.on('node-check-discord-open-client', nodeCheckDiscordClientStatus);
// Requested to get the current listening system
socket.on('node-check-current-system', nodeCheckCurrentSystem);
return socket;
}