From 0fdcb810aea3b2be8f40d0303f103945ede4576b Mon Sep 17 00:00:00 2001 From: Quentin ANIERE Date: Tue, 26 Apr 2022 22:07:06 +0200 Subject: [PATCH] Cleaned code --- .dockerignore | 1 + index.js | 66 +++++++++++++++++++++++++++------------------------ package.json | 4 ++++ 3 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/index.js b/index.js index 31b186e..dea9105 100644 --- a/index.js +++ b/index.js @@ -4,23 +4,6 @@ const express = require("express"); const socketio = require("socket.io"); const basicAuth = require("express-basic-auth"); -const app = express(); - -const https_options = { - key: fs.readFileSync("./privkey.pem"), - cert: fs.readFileSync("./fullchain.pem"), - requestCert: false, - rejectUnauthorized: false -} -const socketHTTPSServer = https.createServer(https_options, app); -const webHTTPSServer = https.createServer(https_options, app); - -const io = socketio(socketHTTPSServer, { - cors: { - origin: "https://danby.aniere.fr", - }, - secure: true -}); function getCurrentTime() { @@ -49,45 +32,65 @@ function log(message) { } +const app = express(); //The base http server, use by socketio and https serveur + +const https_options = { + key: fs.readFileSync("./privkey.pem"), + cert: fs.readFileSync("./fullchain.pem"), + requestCert: false, + rejectUnauthorized: false +} + +const socketHTTPSServer = https.createServer(https_options, app); +const webHTTPSServer = https.createServer(https_options, app); + +const io = socketio(socketHTTPSServer, { + cors: { + origin: "https://danby.aniere.fr", + }, + secure: true +}); usersConnected = 0; lastUsername = 0; messagesHistory = [] io.on("connection", (client) => { - - usersConnected++; - io.sockets.emit("update-count", {value: usersConnected}); - + client.on("disconnect", () => { usersConnected--; io.sockets.emit("update-count", {value: usersConnected}); - log(`${client.usernameDenis} has disconnected`); + + log(`${client.denisFanNumber} has disconnected`); }); client.on("message", (data) => { if(data.message.toLowerCase().includes("denis")) { - io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message}); - log(`${client.usernameDenis} : ${data.message}`); - messagesHistory.push([client.usernameDenis, data.message]); + io.sockets.emit("new-message", {author: client.denisFanNumber, message: data.message}); + log(`${client.denisFanNumber} : ${data.message}`); + + messagesHistory.push([client.denisFanNumber, data.message]); if(messagesHistory.length > 20) { messagesHistory.shift(); } } else { client.emit("warning"); - log(`${client.usernameDenis} : ${data.message} (INVALID)`); + log(`${client.denisFanNumber} : ${data.message} (INVALID)`); } }); lastUsername++; - client.usernameDenis = "DenisFan" + lastUsername; - client.emit("username", {username: "DenisFan" + lastUsername}); - log(`${client.usernameDenis} is now connected`); + usersConnected++; + io.sockets.emit("update-count", {value: usersConnected}); + + client.denisFanNumber = "DenisFan" + lastUsername; + client.emit("username", {username: "DenisFan" + lastUsername}); client.emit("previous-messages", {messages: messagesHistory}); - + + log(`${client.denisFanNumber} is now connected`); }); @@ -96,8 +99,9 @@ app.use(express.static("src")); app.get("/clear", basicAuth({users:{"quentin": "BlanquerCaca89"}, challenge: true}), (req, res) => { messagesHistory = []; res.send("message history was cleared"); - log("Admin cleared messages history"); io.sockets.emit("refresh"); + + log("Admin cleared messages history"); }); app.listen(80, () => { diff --git a/package.json b/package.json index 92977e2..298e05f 100644 --- a/package.json +++ b/package.json @@ -7,5 +7,9 @@ "express": "^4.17.3", "express-basic-auth": "^1.2.1", "socketio": "^1.0.0" + }, + + "scripts": { + "run": "node index.js" } }