Last active
January 22, 2022 17:28
-
-
Save Spiikesan/5ae83c80469ae6caac79e6318d2628b0 to your computer and use it in GitHub Desktop.
KillFeed event generator using tail logs of Pavlov server.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tail = require('tail').Tail | |
const net = require('net') | |
util = require('util'); | |
const connectMessage = '{"ServerName":"' + process.argv[4] + '"}\n'; | |
if (process.argv.length < 3) { | |
console.log("Usage: " + process.argv[1] + " <PavlovServerLogFile> <ServerListeningPort> <ServerName>"); | |
process.exit(1); | |
} | |
config = {follow:true}; | |
tail = new Tail(process.argv[2], config); | |
var partialObject = null; | |
var isReceiving = false; | |
if (!tail) { | |
console.log("ERREUR : Fichier " + process.argv[2] + " non trouvé"); | |
process.exit(1); | |
} | |
const connectedSockets = new Set(); | |
connectedSockets.broadcast = function (data) { | |
for (let socket of this) { | |
socket.write(data); | |
} | |
} | |
const server = net.createServer((c) => { | |
console.log("Client connected : ", c.remoteAddress); | |
connectedSockets.add(c); | |
c.on("end", () => { | |
console.log("Client disconnected : ", c.remoteAddress); | |
connectedSockets.delete(c); | |
}); | |
c.write(connectMessage); | |
}); | |
server.on('error', (err) => { | |
console.log("A server error happened: ", err); | |
}); | |
server.listen(process.argv[3], () => { | |
console.log("Server bound to " + process.argv[3]); | |
}); | |
tail.on("line", (data) => { | |
if (isReceiving) { | |
partialObject += data; | |
if (data === "}") { | |
let obj = partialObject; | |
partialObject = null; | |
isReceiving = false; | |
processFinalObject(obj); | |
} | |
} else if (data.includes("StatManagerLog")) { | |
isReceiving = true; | |
partialObject = "{"; | |
} | |
}); | |
tail.on("error", (error) => { | |
console.log("Error : ", error); | |
}); | |
function processFinalObject(obj) { | |
try { | |
jsonObject = JSON.stringify(JSON.parse(obj)); | |
console.log("Object generated: ", jsonObject); | |
connectedSockets.broadcast(jsonObject + "\n"); | |
} catch (e) { | |
console.log("Error parsing object ", obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment