2024-06-02 17:14:03 +02:00

57 lines
1.6 KiB
JavaScript

const express = require('express');
const router = express.Router();
const db = require("../dependencies");
router.post("/reset", (req, res) => {
db.prepare("BEGIN TRANSACTION; DELETE FROM accounts; DELETE FROM transaction_entries; DELETE FROM transactions COMMIT;")
})
var socket
function socketConnectionRequest(req, res, next) {
const headers = {
'Content-Type': 'text/event-stream', // To tell client, it is event stream
'Connection': 'keep-alive', // To tell client, not to close connection
};
res.writeHead(200, headers);
res.write('data: Connection Established, We\'ll now start receiving messages from the server.\n')
socket = res
console.log('New connection established')
}
const update = new Map()
async function mysse(req, res) {
console.log('Got /events');
res.set({
'Cache-Control': 'no-cache',
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive'
});
res.flushHeaders();
// Tell the client to retry every 10 seconds if connectivity is lost
res.write('retry: 10000\n\n');
update.set(Math.random(), res)
console.log("connected")
res.write("data: update\n\n")
// while (true ) {
// await new Promise(resolve => setTimeout(resolve, 1000));
// // console.log('Emit', ++count);
// // Emit an SSE that contains the current 'count' as a string
// res.write(`data: ${update}\n\n`);
// // update = false;
// }
};
function set_update() {
update = true;
}
module.exports = { router, mysse, update};