mirror of
https://github.com/BreizhHardware/Jellystat.git
synced 2026-01-18 16:27:20 +01:00
31 lines
708 B
JavaScript
31 lines
708 B
JavaScript
// ws.js
|
|
const socketIO = require("socket.io");
|
|
|
|
let io; // Store the socket.io server instance
|
|
|
|
const setupWebSocketServer = (server, namespacePath) => {
|
|
io = socketIO(server, { path: namespacePath + "/socket.io" }); // Create the socket.io server
|
|
|
|
io.on("connection", (socket) => {
|
|
console.log("Client connected to namespace:", namespacePath);
|
|
|
|
socket.on("message", (message) => {
|
|
console.log(`Received: ${message}`);
|
|
});
|
|
});
|
|
};
|
|
|
|
const sendToAllClients = (message) => {
|
|
if (io) {
|
|
io.emit("message", message);
|
|
}
|
|
};
|
|
|
|
const sendUpdate = (tag, message) => {
|
|
if (io) {
|
|
io.emit(tag, message);
|
|
}
|
|
};
|
|
|
|
module.exports = { setupWebSocketServer, sendToAllClients, sendUpdate };
|