Initial commit.
This commit is contained in:
commit
2856c03b46
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
node_modules/
|
||||||
|
.env
|
13
eslint.config.mjs
Normal file
13
eslint.config.mjs
Normal 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
2149
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
package.json
Normal file
26
package.json
Normal 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
12
public/index.html
Normal 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
22
public/main.js
Normal 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
10
src/app.js
Normal 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
15
src/server.js
Normal 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}.`);
|
||||||
|
});
|
9
src/services/chatService.js
Normal file
9
src/services/chatService.js
Normal 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
12
src/sockets/chatSocket.js
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user