-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·92 lines (74 loc) · 2.28 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import express from "express";
import expressWs from "express-ws";
import { Game } from "./src/game.js";
import { Codec } from "./src/message.js";
import https from "https";
import fs from "fs";
import http from "http";
const app = express();
// The sockets of the connected players
let sockets = [];
const codec = new Codec();
// Initialize the game
const game = new Game((message) => {
// TODO: Broadcast the message to the connected browsers
for (const socket of sockets) {
if (socket.readyState === socket.OPEN) {
socket.send(codec.encode(message));
}
}
});
setInterval(() => {
game.move();
}, 10);
// Serve the public directory
app.use(express.static("public"));
// Serve the src directory
app.use("/src", express.static("src"));
// Serve the src directory
app.use("/test", express.static("test"));
// Serve the jsdoc directory
app.use("/doc", express.static("out"));
// Serve the dist directory
app.use("/dist", express.static("dist"));
const httpServer = http.createServer(app);
const httpsServer = https.createServer(
{
key: fs.readFileSync("./luca-Aspire-A515-54G-key.pem"),
cert: fs.readFileSync("./luca-Aspire-A515-54G.pem"),
},
app
);
expressWs(app, httpsServer);
httpsServer.listen(3001, () => {
console.log("HTTPS Server running on port 3001");
});
httpServer.listen(3000, () => {
console.log("HTTP Server running on port 3000");
});
// Websocket game events
app.ws("/", (socket, req) => {
sockets.push(socket);
const query = req.url.split("?")[1]; // récupère la query string
const params = new URLSearchParams(query); // crée un objet URLSearchParams à partir de la query string
const id = params.get("id"); // récupère la valeur de la query id
let player;
// Si la connection vient d'un remote controller
if (id !== null) {
player = parseInt(id);
} else {
player = game.join();
}
socket.on("message", (string) => {
const message = codec.decode(string);
console.log(message);
game.onMessage(player, message);
});
socket.on("close", () => {
// Si ce n'est pas un remote controller
if (id === null) {
game.quit(player);
sockets = sockets.filter((s) => s !== socket);
}
});
});