Cleaned code

This commit is contained in:
Quentin ANIERE 2022-04-26 22:07:06 +02:00
parent 427bff7aa9
commit 0fdcb810ae
3 changed files with 40 additions and 31 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
node_modules

View File

@ -4,23 +4,6 @@ const express = require("express");
const socketio = require("socket.io"); const socketio = require("socket.io");
const basicAuth = require("express-basic-auth"); 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() { 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; usersConnected = 0;
lastUsername = 0; lastUsername = 0;
messagesHistory = [] messagesHistory = []
io.on("connection", (client) => { io.on("connection", (client) => {
usersConnected++;
io.sockets.emit("update-count", {value: usersConnected});
client.on("disconnect", () => { client.on("disconnect", () => {
usersConnected--; usersConnected--;
io.sockets.emit("update-count", {value: usersConnected}); io.sockets.emit("update-count", {value: usersConnected});
log(`${client.usernameDenis} has disconnected`);
log(`${client.denisFanNumber} has disconnected`);
}); });
client.on("message", (data) => { client.on("message", (data) => {
if(data.message.toLowerCase().includes("denis")) { 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) { if(messagesHistory.length > 20) {
messagesHistory.shift(); messagesHistory.shift();
} }
} else { } else {
client.emit("warning"); client.emit("warning");
log(`${client.usernameDenis} : ${data.message} (INVALID)`); log(`${client.denisFanNumber} : ${data.message} (INVALID)`);
} }
}); });
lastUsername++; lastUsername++;
client.usernameDenis = "DenisFan" + lastUsername; usersConnected++;
client.emit("username", {username: "DenisFan" + lastUsername});
log(`${client.usernameDenis} is now connected`);
io.sockets.emit("update-count", {value: usersConnected});
client.denisFanNumber = "DenisFan" + lastUsername;
client.emit("username", {username: "DenisFan" + lastUsername});
client.emit("previous-messages", {messages: messagesHistory}); 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) => { app.get("/clear", basicAuth({users:{"quentin": "BlanquerCaca89"}, challenge: true}), (req, res) => {
messagesHistory = []; messagesHistory = [];
res.send("message history was cleared"); res.send("message history was cleared");
log("Admin cleared messages history");
io.sockets.emit("refresh"); io.sockets.emit("refresh");
log("Admin cleared messages history");
}); });
app.listen(80, () => { app.listen(80, () => {

View File

@ -7,5 +7,9 @@
"express": "^4.17.3", "express": "^4.17.3",
"express-basic-auth": "^1.2.1", "express-basic-auth": "^1.2.1",
"socketio": "^1.0.0" "socketio": "^1.0.0"
},
"scripts": {
"run": "node index.js"
} }
} }