From 6a755ec5afb11b6fd9dfd7b73fc070016cf480b0 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 11 Jul 2019 17:55:15 -0400 Subject: [PATCH] Add log replay on websocket connect --- src/StatusScreen.vue | 2 +- src/server.js | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/StatusScreen.vue b/src/StatusScreen.vue index 0a1df7c..70b326f 100644 --- a/src/StatusScreen.vue +++ b/src/StatusScreen.vue @@ -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); diff --git a/src/server.js b/src/server.js index bdee80b..1fc7145 100644 --- a/src/server.js +++ b/src/server.js @@ -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)); +});