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) {
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);
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")
);
function datestamp(date) {
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
}
client.on("data", function(data) {
let str = data.toString("utf8").replace(/^\s+|\s+$/g, "");
console.log(str);
@ -26,24 +30,22 @@ client.on("data", function(data) {
if (!packet.startsWith("#")) {
// ignore comments
let date = new Date();
let datestamp =
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
let data = [datestamp, packet];
console.log(data);
fs.appendFile(
"log" + datestamp + ".json",
JSON.stringify(data) + ",\n",
`log${datestamp(date)}.json`,
JSON.stringify([date, packet]) + "\n",
err => {
if (err) throw err;
}
);
wss.broadcast(JSON.stringify(data));
wss.broadcast(JSON.stringify([date, packet]));
}
});
});
// wss.on('connection', ws => {
// let datestamp = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
// fs.readFileSync("log" + datestamp + ".json")
// .toString().split('\n').forEach(line => ws.send(line));
// });
wss.on("connection", ws => {
let date = new Date();
fs.readFileSync(`log${datestamp(date)}.json`)
.toString()
.split("\n")
.forEach(line => ws.send(line));
});