increase timeout of ws

This commit is contained in:
MuslemRahimi 2024-09-19 20:49:30 +02:00
parent 58de4b8a07
commit 4e3576ac78
2 changed files with 31 additions and 84 deletions

View File

@ -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) {

View File

@ -1,10 +1,9 @@
// Declare a route
module.exports = function (fastify, opts, done) {
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;
@ -17,23 +16,31 @@ module.exports = function (fastify, opts, done) {
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 });
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) {
} catch (e) {
//console.log(e)
output = await pb.collection("watchlist").create(serialize({'user': userId, 'ticker': JSON.stringify([ticker]), 'title': 'Favorites' }));
output = await pb.collection("watchlist").create(
serialize({
user: userId,
ticker: JSON.stringify([ticker]),
title: "Favorites",
})
);
}
reply.send({ items: output })
reply.send({ items: output });
});
done();