Add log replay on websocket connect

This commit is contained in:
Adam Goldsmith 2019-07-11 17:55:15 -04:00
parent 7dd98d9a10
commit 6a755ec5af
2 changed files with 15 additions and 13 deletions

View File

@ -76,7 +76,7 @@ export default {
handleMessage(packet) { handleMessage(packet) {
let message = this.parser.parse(packet[1]); let message = this.parser.parse(packet[1]);
message.date = new Date(); // TODO: use data[0] instead message.date = new Date(packet[0]);
console.log(message); console.log(message);
this.messages.push(message); this.messages.push(message);

View File

@ -17,6 +17,10 @@ client.connect(14580, "rotate.aprs2.net", () =>
client.write("user KC1GDW pass -1 filter r/43.90/-72.15/75\r\n") client.write("user KC1GDW pass -1 filter r/43.90/-72.15/75\r\n")
); );
function datestamp(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
client.on("data", function(data) { client.on("data", function(data) {
let str = data.toString("utf8").replace(/^\s+|\s+$/g, ""); let str = data.toString("utf8").replace(/^\s+|\s+$/g, "");
console.log(str); console.log(str);
@ -26,24 +30,22 @@ client.on("data", function(data) {
if (!packet.startsWith("#")) { if (!packet.startsWith("#")) {
// ignore comments // ignore comments
let date = new Date(); let date = new Date();
let datestamp =
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
let data = [datestamp, packet];
console.log(data);
fs.appendFile( fs.appendFile(
"log" + datestamp + ".json", `log${datestamp(date)}.json`,
JSON.stringify(data) + ",\n", JSON.stringify([date, packet]) + "\n",
err => { err => {
if (err) throw err; if (err) throw err;
} }
); );
wss.broadcast(JSON.stringify(data)); wss.broadcast(JSON.stringify([date, packet]));
} }
}); });
}); });
// wss.on('connection', ws => { wss.on("connection", ws => {
// let datestamp = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); let date = new Date();
// fs.readFileSync("log" + datestamp + ".json") fs.readFileSync(`log${datestamp(date)}.json`)
// .toString().split('\n').forEach(line => ws.send(line)); .toString()
// }); .split("\n")
.forEach(line => ws.send(line));
});