diff --git a/fastify/app.js b/fastify/app.js index 6003dda..bd83c8f 100755 --- a/fastify/app.js +++ b/fastify/app.js @@ -188,7 +188,7 @@ fastify.register(async function (fastify) { isSend = true; setTimeout(() => { isSend = false; - }, 800); + }, 2000); } } } catch (error) { @@ -278,7 +278,7 @@ fastify.register(async function (fastify) { isSend = true; setTimeout(() => { isSend = false; - }, 800); + }, 2000); //wait(2000); } @@ -364,66 +364,6 @@ fastify.register(async function (fastify) { ); }); -fastify.register(async function (fastify) { - fastify.get( - "/options-zero-dte-reader", - { websocket: true }, - (connection, req) => { - let jsonData; - let sendInterval; - - // Function to send data to the client - const sendData = async () => { - const filePath = path.join( - __dirname, - "../app/json/options-flow/zero-dte/data.json" - ); - try { - if (fs.existsSync(filePath)) { - const fileData = fs.readFileSync(filePath, "utf8"); - jsonData = JSON.parse(fileData); - connection.socket.send(JSON.stringify(jsonData)); - } else { - console.error("File not found:", filePath); - clearInterval(sendInterval); - console.error("Connection closed"); - throw new Error("This is an intentional uncaught exception!"); - } - } catch (err) { - console.error("Error sending data to client:", err); - } - }; - - // Send data to the client initially - sendData(); - - // Start sending data periodically - sendInterval = setInterval(sendData, 5000); - - // Handle client disconnect - connection.socket.on("close", () => { - console.log("Client disconnected"); - connection?.socket?.close(); - clearInterval(sendInterval); - }); - - // Handle server crash cleanup - const closeHandler = () => { - console.log("Server is closing. Cleaning up resources..."); - clearInterval(sendInterval); - connection?.socket?.close(); - }; - - // Add close handler to process event - process.on("exit", closeHandler); - process.on("SIGINT", closeHandler); - process.on("SIGTERM", closeHandler); - process.on("uncaughtException", closeHandler); - process.on("unhandledRejection", closeHandler); - } - ); -}); - // Function to start the server function startServer() { if (!serverRunning) { diff --git a/fastify/update-watchlist/server.js b/fastify/update-watchlist/server.js index 9dd8c74..18e1335 100755 --- a/fastify/update-watchlist/server.js +++ b/fastify/update-watchlist/server.js @@ -1,10 +1,9 @@ // Declare a route module.exports = function (fastify, opts, done) { - - const pb = opts.pb; - const serialize = opts.serialize; + const pb = opts.pb; + const serialize = opts.serialize; - fastify.post('/update-watchlist', async (request, reply) => { + fastify.post("/update-watchlist", async (request, reply) => { const data = request.body; const userId = data?.userId; @@ -13,28 +12,36 @@ module.exports = function (fastify, opts, done) { let output; try { - const watchList = await pb.collection("watchlist").getOne(watchListId); + const watchList = await pb.collection("watchlist").getOne(watchListId); - if (watchList?.ticker?.includes(ticker)) { + if (watchList?.ticker?.includes(ticker)) { // Remove ticker from the watchlist. - const newTickerList = watchList?.ticker.filter(item => item !== ticker); - output = await pb.collection("watchlist").update(watchListId, { ticker: newTickerList }); - } else { + const newTickerList = watchList?.ticker.filter( + (item) => item !== ticker + ); + output = await pb + .collection("watchlist") + .update(watchListId, { ticker: newTickerList }); + } else { // Add ticker to the watchlist. const newTickerList = [...watchList?.ticker, ticker]; - output = await pb.collection("watchlist").update(watchListId, { ticker: newTickerList }); - } - + output = await pb + .collection("watchlist") + .update(watchListId, { ticker: newTickerList }); + } + } catch (e) { + //console.log(e) + output = await pb.collection("watchlist").create( + serialize({ + user: userId, + ticker: JSON.stringify([ticker]), + title: "Favorites", + }) + ); } - catch(e) { - //console.log(e) - output = await pb.collection("watchlist").create(serialize({'user': userId, 'ticker': JSON.stringify([ticker]), 'title': 'Favorites' })); - } - - reply.send({ items: output }) + reply.send({ items: output }); + }); - }); - - done(); -}; \ No newline at end of file + done(); +};