Files
2025-06-12 11:15:28 +02:00

65 lines
1.6 KiB
JavaScript

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
}