Map socket comm
This commit is contained in:
+14
-1
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user