increase timeout of ws
This commit is contained in:
parent
58de4b8a07
commit
4e3576ac78
@ -188,7 +188,7 @@ fastify.register(async function (fastify) {
|
|||||||
isSend = true;
|
isSend = true;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
isSend = false;
|
isSend = false;
|
||||||
}, 800);
|
}, 2000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -278,7 +278,7 @@ fastify.register(async function (fastify) {
|
|||||||
isSend = true;
|
isSend = true;
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
isSend = false;
|
isSend = false;
|
||||||
}, 800);
|
}, 2000);
|
||||||
|
|
||||||
//wait(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 to start the server
|
||||||
function startServer() {
|
function startServer() {
|
||||||
if (!serverRunning) {
|
if (!serverRunning) {
|
||||||
|
|||||||
@ -1,10 +1,9 @@
|
|||||||
// Declare a route
|
// Declare a route
|
||||||
module.exports = function (fastify, opts, done) {
|
module.exports = function (fastify, opts, done) {
|
||||||
|
|
||||||
const pb = opts.pb;
|
const pb = opts.pb;
|
||||||
const serialize = opts.serialize;
|
const serialize = opts.serialize;
|
||||||
|
|
||||||
fastify.post('/update-watchlist', async (request, reply) => {
|
fastify.post("/update-watchlist", async (request, reply) => {
|
||||||
const data = request.body;
|
const data = request.body;
|
||||||
|
|
||||||
const userId = data?.userId;
|
const userId = data?.userId;
|
||||||
@ -17,23 +16,31 @@ module.exports = function (fastify, opts, done) {
|
|||||||
|
|
||||||
if (watchList?.ticker?.includes(ticker)) {
|
if (watchList?.ticker?.includes(ticker)) {
|
||||||
// Remove ticker from the watchlist.
|
// Remove ticker from the watchlist.
|
||||||
const newTickerList = watchList?.ticker.filter(item => item !== ticker);
|
const newTickerList = watchList?.ticker.filter(
|
||||||
output = await pb.collection("watchlist").update(watchListId, { ticker: newTickerList });
|
(item) => item !== ticker
|
||||||
|
);
|
||||||
|
output = await pb
|
||||||
|
.collection("watchlist")
|
||||||
|
.update(watchListId, { ticker: newTickerList });
|
||||||
} else {
|
} else {
|
||||||
// Add ticker to the watchlist.
|
// Add ticker to the watchlist.
|
||||||
const newTickerList = [...watchList?.ticker, ticker];
|
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)
|
//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();
|
done();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user