Initial commit.

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

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules/
.env

13
eslint.config.mjs Normal file
View File

@ -0,0 +1,13 @@
import js from "@eslint/js";
import globals from "globals";
import json from "@eslint/json";
import css from "@eslint/css";
import { defineConfig } from "eslint/config";
export default defineConfig([
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"] },
{ files: ["**/*.{js,mjs,cjs}"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
{ files: ["**/*.json"], plugins: { json }, language: "json/json", extends: ["json/recommended"] },
{ files: ["**/*.css"], plugins: { css }, language: "css/css", extends: ["css/recommended"] },
]);

2149
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "gub",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.5.0",
"express": "^5.1.0",
"ws": "^8.18.2"
},
"devDependencies": {
"@eslint/css": "^0.8.1",
"@eslint/js": "^9.27.0",
"@eslint/json": "^0.12.0",
"eslint": "^9.27.0",
"globals": "^16.1.0",
"nodemon": "^3.1.10",
"prettier": "^3.5.3"
}
}

12
public/index.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<title>Gub</title>
</head>
<body>
<div id="messages"></div>
<input id="input" autocomplete="off" />
<button onclick="sendMessage()">Send</button>
<script src="main.js"></script>
</body>
</html>

22
public/main.js Normal file
View File

@ -0,0 +1,22 @@
const ws = new WebSocket(`ws://${location.host}`);
const messagesDiv = document.getElementById("messages");
const input = document.getElementById("input");
ws.onmessage = (event) => {
const msg = document.createElement("div");
msg.textContent = event.data;
messagesDiv.appendChild(msg);
messagesDiv.scrollTop = messagesDiv.scrollHeight;
};
function sendMessage() {
const text = input.value;
if (text.trim()) {
ws.send(text);
input.value = "";
}
}
input.addEventListener("keypress", (e) => {
if (e.key === "Enter") sendMessage();
});

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;