DenisSlider2000/index.js

87 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-04-09 00:32:32 +02:00
const fs = require("fs");
const https = require("https");
const express = require("express");
const socketio = require("socket.io");
2022-04-07 19:09:47 +02:00
const app = express();
2022-04-07 19:37:26 +02:00
const https_options = {
2022-04-09 00:32:32 +02:00
key: fs.readFileSync("./privkey.pem"),
cert: fs.readFileSync("./fullchain.pem"),
2022-04-07 19:37:26 +02:00
requestCert: false,
rejectUnauthorized: false
}
2022-04-11 12:34:40 +02:00
const socketHTTPSServer = https.createServer(https_options, app);
const webHTTPSServer = https.createServer(https_options, app);
2022-04-07 19:37:26 +02:00
2022-04-11 12:34:40 +02:00
const io = socketio(socketHTTPSServer, {
2022-04-07 19:37:26 +02:00
cors: {
origin: "https://danby.aniere.fr",
},
secure: true
});
2022-04-07 19:09:47 +02:00
2022-04-12 13:38:33 +02:00
function getCurrentTime() {
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
return h + ":" + m + ":" + s;
};
2022-04-12 10:43:11 +02:00
usersConnected = 0;
lastUsername = 0;
2022-04-07 19:09:47 +02:00
io.on("connection", (client) => {
usersConnected++;
2022-04-12 10:43:11 +02:00
io.sockets.emit("update-count", {value: usersConnected});
2022-04-12 13:38:33 +02:00
2022-04-07 19:09:47 +02:00
client.on("disconnect", () => {
usersConnected--;
io.sockets.emit("update-count", {value: usersConnected});
2022-04-12 17:59:28 +02:00
console.log(getCurrentTime() + ": Client disconnection (" + client.usernameDenis + ")");
2022-04-07 19:09:47 +02:00
});
2022-04-12 10:43:11 +02:00
client.on("message", (data) => {
2022-04-12 13:29:46 +02:00
if(data.message.toLowerCase().includes("denis")) {
io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message});
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": (" + client.usernameDenis + ") : " + data.message);
2022-04-12 13:29:46 +02:00
} else {
client.emit("warning");
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": (" + client.usernameDenis + ") : " + data.message + "(invalid)");
2022-04-12 13:29:46 +02:00
}
2022-04-12 10:43:11 +02:00
});
lastUsername++;
client.usernameDenis = "DenisFan" + lastUsername;
client.emit("username", {username: "DenisFan" + lastUsername});
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": Client connection (" + client.usernameDenis + ")");
2022-04-12 10:43:11 +02:00
2022-04-07 19:09:47 +02:00
});
2022-04-11 12:34:40 +02:00
app.use(express.static("src"));
app.listen(80, () => {
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": Web server started on port 80")
2022-04-11 12:34:40 +02:00
});
webHTTPSServer.listen(443, () => {
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": Web HTTPS server started on port 443");
2022-04-11 12:34:40 +02:00
});
socketHTTPSServer.listen(3000, () => {
2022-04-12 13:38:33 +02:00
console.log(getCurrentTime() + ": Socket HTTPS server started on port 3000");
2022-04-07 19:09:47 +02:00
});