Map socket comm

This commit is contained in:
2025-06-12 11:15:28 +02:00
parent de15de87a9
commit 5fe201182a
5 changed files with 151 additions and 48 deletions
+5 -6
View File
@@ -6,6 +6,7 @@ const express = require('express');
const app_conf = require("../config/app.conf.js");
const map_conf = require("../config/map.conf.js");
const messages = require("../config/messages.conf.js");
const socket = require("../utils/socket.util.js");
const map = require("../utils/map.util.js");
@@ -29,6 +30,7 @@ router.get("/disable", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "");
socket.broadcastMap(null);
res.status(200).send();
});
@@ -41,6 +43,7 @@ router.get("/enable", async (req, res) => {
if (requested_map && map.mapExists(requested_map)) {
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), requested_map);
socket.broadcastMap(requested_map);
res.status(200).send();
} else {
@@ -62,6 +65,7 @@ router.delete("/remove", async (req, res) => {
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
if (selected_map_name == element) {
fs.writeFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "");
socket.broadcastMap(null);
}
}
res.status(200).send();
@@ -195,12 +199,7 @@ router.get("/", async (req, res) => {
try {
const maps = await map.getAvailableMaps();
var selected_map_name = "";
// Check if a map has been selected to show it
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
}
var selected_map_name = map.getSelectedMap();
// Construct the maps table
var table_data = "";
+14 -1
View File
@@ -5,6 +5,18 @@ const uuid = require("uuid");
const map_conf = require("../config/map.conf");
const logger = require("./logger.util");
// Returns map filename or ""
function getSelectedMap() {
var selected_map_name = "";
// Check if a map has been selected to show it
if (fs.existsSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"))) {
selected_map_name = fs.readFileSync(path.join(path.dirname(require.main.filename), map_conf.map_storage_path, "selected"), "utf-8");
}
return(selected_map_name);
}
// Creates new map
async function addMap(filename, meta, throttle, motor) {
var total = "";
@@ -201,5 +213,6 @@ module.exports = {
parseMapJson,
mapExists,
updateMap,
addMap
addMap,
getSelectedMap
};
+65
View File
@@ -0,0 +1,65 @@
const net = require("net");
const fs = require("fs");
const path = require("path");
const map_conf = require("../config/map.conf");
const map = require("../utils/map.util");
const logger = require("../utils/logger.util");
const can_conf = require("../config/can.conf")
const SOCKET_PATH = path.join(path.dirname(require.main.filename), map_conf.map_storage_path, can_conf.socket_filename);
var clients = [];
if (fs.existsSync(SOCKET_PATH)) {
logger.info("Removing existing map socket...");
fs.unlinkSync(SOCKET_PATH);
}
function broadcastMap(filename) {
logger.info("Map updated. Broadcasting new filename to map socket...");
let message = JSON.stringify({ "map": filename });
for (c of clients) {
c.write(message);
}
}
function closeSocket() {
for (c of clients) {
c.end();
}
logger.info("Closing map socket.");
server.close();
}
const server = net.createServer((client) => {
// send selected map
logger.info("Client connected to map socket.");
var map_filename = map.getSelectedMap();
if (map_filename === "") {
map_filename = null;
}
client.write(JSON.stringify({"map": map_filename}));
// client array
clients.push(client);
client.on("end", () => {
logger.info("Client disconnected from map socket.");
const index = clients.indexOf(client);
if (index !== -1) {
clients.splice(index, 1);
}
});
});
server.listen(SOCKET_PATH, () => {
logger.info("Started map socket");
});
module.exports = {
broadcastMap,
closeSocket
}