more/better comments, splitting up functions and fixing chat output

This commit is contained in:
Baipyrus 2023-01-19 12:57:44 +01:00
parent 0ffc9c16a2
commit 3832413c23

View File

@ -1,11 +1,16 @@
// Global logging toggles
const fileLogging = false;
const consoleLogging = true;
// Imports
const fs = require('fs');
const http = require('http');
const crypto = require('crypto');
const express = require('express');
// Global server setup
const app = express();
const clientPath = __dirname+'/../html';
console.log('Serving static from ' + clientPath);
@ -13,8 +18,12 @@ app.use(express.json());
app.use(express.static(clientPath));
const server = http.createServer(app);
// User data
const users = [];
// Incoming user connection
app.get('/connect', (req, res) => {
// Generate id, save user
const id = crypto.randomUUID();
users.push({
socket: req.socket,
@ -23,26 +32,32 @@ app.get('/connect', (req, res) => {
name: "",
id
});
// Log connection, send back id
if (consoleLogging)
console.log(`User with ID '${id}' connected.`);
res.status(200).json({ id });
});
// User has disconnected
app.post('/disconnect', (req, res) => {
const { id } = req.body;
for (let i = 0; i < users.length; i++)
if (users[i].id === id) {
// If id is valid, delete all user data
if (consoleLogging)
console.log(`User with ID '${id}' disconnected.`);
users.splice(i, 1);
break;
}
// Confirm disconnect
res.status(200).send();
});
// User wants to change name
app.post('/nickname', (req, res) => {
const { id, name } = req.body;
// Check if name is taken
let success = true;
for (const u of users)
if (u.name === name) {
@ -51,6 +66,7 @@ app.post('/nickname', (req, res) => {
}
if (success) {
// Change name if not taken
for (const u of users)
if (u.id === id) {
if (consoleLogging)
@ -60,12 +76,15 @@ app.post('/nickname', (req, res) => {
}
}
// Confirm renaming
res.status(200).json({ success });
});
// User is pulling messages
app.post('/getChat', (req, res) => {
const { id } = req.body
// Get users' name
let name = "";
for (const u of users)
if (u.id === id) {
@ -73,11 +92,13 @@ app.post('/getChat', (req, res) => {
break;
}
// If no name was found, user cannot send or receive messages
if (name === "") {
res.status(401).send();
return;
}
// Go through users' chats, retrieve messages, save by username
let chats = {};
for (const u of users)
if (name in u.chats) {
@ -86,19 +107,25 @@ app.post('/getChat', (req, res) => {
chats[u.name] = u.chats[name].splice(0, len);
}
// No chat messages were found
if (Object.keys(chats).length === 0) {
res.status(204).send();
return;
}
// Log if messages were delivered
if (consoleLogging)
console.log(`Delivered messages to user with ID '${id}'.`);
// Send back data
res.status(200).json(chats);
});
// User sent new message
app.post('/message', (req, res) => {
const { id, message, name } = req.body;
// Search for senders' name, save message
let ownName;
for (const u of users)
if (u.id === id) {
@ -109,6 +136,7 @@ app.post('/message', (req, res) => {
break;
}
// Log message for debugging
let user = `User '${ownName}'`;
if (!ownName)
user = ownName = "Anonymous"
@ -117,17 +145,21 @@ app.post('/message', (req, res) => {
console.log(`${user} sent message '${message}'.${idStr}`);
}
// Confirm process
res.status(200).send();
});
// Internal server error
server.on('error', err => {
console.error('Internal server error', err);
});
// Server has closed
server.on('close', () => {
console.log("Shutting down ...");
console.log('Shutting down server ...');
});
// Listen on port 3000 for webserver connection
server.listen(3000, () => {
console.log('Server running on port 3000');
});