Initial commit.

This commit is contained in:
2025-05-23 16:36:20 -04:00
commit 2856c03b46
10 changed files with 2270 additions and 0 deletions

10
src/app.js Normal file
View File

@@ -0,0 +1,10 @@
const express = require("express");
const path = require("path");
require("dotenv").config();
const app = express();
// Serve static files in /public.
app.use(express.static(path.join(__dirname, "..", "public")));
module.exports = app;

15
src/server.js Normal file
View File

@@ -0,0 +1,15 @@
const http = require("http");
const WebSocket = require("ws");
const app = require("./app");
const setupChatSocket = require("./sockets/chatSocket");
const PORT = process.env.PORT || 3000;
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
setupChatSocket(wss);
server.listen(PORT, () => {
console.log(`Server is running at http://127.0.1:${PORT}.`);
});

View File

@@ -0,0 +1,9 @@
function broadcast(wss, data, sender) {
wss.clients.forEach((client) => {
if (client !== sender && client.readyState === 1) {
client.send(data);
}
});
}
module.exports = { broadcast };

12
src/sockets/chatSocket.js Normal file
View File

@@ -0,0 +1,12 @@
const chatService = require("../services/chatService");
function setupChatSocket(wss) {
wss.on("connection", (ws) => {
ws.send("Welcome.");
ws.on("message", (message) => {
chatService.broadcast(wss, message, ws);
});
});
}
module.exports = setupChatSocket;