var dateNow = 0;
var saveTimeout = null;
+var running = true;
-function saveGames() {
+function saveGames(exit) {
fs.writeFile("games.backup.json", JSON.stringify(games), function (err) {
if (err) {
console.error("ERROR: Cannot save games!");
+ } else if (exit) {
+ console.log("Games saved.");
+ }
+
+ if (exit) {
+ // eslint-disable-next-line no-process-exit
+ process.exit(0);
}
});
: data.length + data
);
- if (!terminate) {
+ if (terminate) {
+ stopKeepAlive(responseAndIsES);
+ } else {
keepAlive(responseAndIsES);
}
}
+function stop() {
+ running = false;
+ console.log("Closing connections and saving the game state...");
+
+ let listeningPlayerCount = 0;
+
+ for (const gameID of Object.keys(games)) {
+ const game = games[gameID];
+ for (const listeningPlayer of game.listeningPlayers) {
+ writeResponse(listeningPlayer, '{"stopping": 2000}', true);
+ }
+ listeningPlayerCount += game.listeningPlayers.length;
+ }
+
+ console.log("Stopped", games.length, "games and ", listeningPlayerCount, " player connections.");
+ saveGames(true);
+}
+
function stopKeepAlive(responseAndIsES) {
if (responseAndIsES.keepAliveTimeout) {
clearTimeout(responseAndIsES.keepAliveTimeout);
});
request.on("end", function () {
+ if (!running) {
+ response.statusCode = 503;
+ response.statusMessage = "Server is stopping";
+ response.end("Server is stopping");
+ return;
+ }
+
if (DEV_ENABLE_SERVING_FILES && !request.url.startsWith("/:")) {
if (request.url === "/") {
request.url = "/index.html";
console.log("Server listening on: http://localhost:%s", port);
});
});
+
+
+process.once("SIGINT", function () {
+ console.log("SIGINT received...");
+ stop();
+});
+
+process.once("SIGTERM", function () {
+ console.log("SIGTERM received...");
+ stop();
+});