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 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,6 +32,24 @@ 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;
@ -56,38 +57,40 @@ 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, () => {

View File

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