61 lines
1.5 KiB
JavaScript
61 lines
1.5 KiB
JavaScript
const fs = require("fs");
|
|
const https = require("https");
|
|
const express = require("express");
|
|
const socketio = require("socket.io");
|
|
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
|
|
});
|
|
|
|
usersConnected = 0;
|
|
lastUsername = 0;
|
|
|
|
io.on("connection", (client) => {
|
|
|
|
usersConnected++;
|
|
io.sockets.emit("update-count", {value: usersConnected});
|
|
|
|
client.on("disconnect", () => {
|
|
usersConnected--;
|
|
io.sockets.emit("update-count", {value: usersConnected});
|
|
});
|
|
|
|
client.on("message", (data) => {
|
|
io.sockets.emit("new-message", {author: client.usernameDenis, message: data.message});
|
|
});
|
|
|
|
lastUsername++;
|
|
client.usernameDenis = "DenisFan" + lastUsername;
|
|
client.emit("username", {username: "DenisFan" + lastUsername});
|
|
|
|
});
|
|
|
|
|
|
app.use(express.static("src"));
|
|
|
|
|
|
app.listen(80, () => {
|
|
console.log("Web server started on port 80")
|
|
});
|
|
|
|
webHTTPSServer.listen(443, () => {
|
|
console.log("Web HTTPS server started on port 443");
|
|
});
|
|
|
|
socketHTTPSServer.listen(3000, () => {
|
|
console.log("Socket HTTPS server started on port 3000");
|
|
});
|