remove id's from options flow wss
This commit is contained in:
parent
0b7b2660de
commit
a53f3ae87b
@ -121,7 +121,6 @@ const WebSocket = require("ws");
|
|||||||
let isSend = false;
|
let isSend = false;
|
||||||
let sendInterval;
|
let sendInterval;
|
||||||
|
|
||||||
/*
|
|
||||||
fastify.register(async function (fastify) {
|
fastify.register(async function (fastify) {
|
||||||
fastify.get("/realtime-data", { websocket: true }, (connection, req) => {
|
fastify.get("/realtime-data", { websocket: true }, (connection, req) => {
|
||||||
// Send a welcome message to the client
|
// Send a welcome message to the client
|
||||||
@ -312,9 +311,8 @@ fastify.register(async function (fastify) {
|
|||||||
(connection, req) => {
|
(connection, req) => {
|
||||||
let jsonData;
|
let jsonData;
|
||||||
let sendInterval;
|
let sendInterval;
|
||||||
let clientIds = [];
|
|
||||||
|
|
||||||
// Function to send filtered data to the client
|
// Function to send data to the client
|
||||||
const sendData = async () => {
|
const sendData = async () => {
|
||||||
const filePath = path.join(
|
const filePath = path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
@ -324,20 +322,7 @@ fastify.register(async function (fastify) {
|
|||||||
if (fs.existsSync(filePath)) {
|
if (fs.existsSync(filePath)) {
|
||||||
const fileData = fs.readFileSync(filePath, "utf8");
|
const fileData = fs.readFileSync(filePath, "utf8");
|
||||||
jsonData = JSON.parse(fileData);
|
jsonData = JSON.parse(fileData);
|
||||||
|
connection.socket.send(JSON.stringify(jsonData));
|
||||||
// Do nothing if clientIds is empty
|
|
||||||
if (clientIds.length === 0) {
|
|
||||||
console.log("Client IDs list is empty, doing nothing.");
|
|
||||||
return; // Exit function if clientIds is empty
|
|
||||||
}
|
|
||||||
|
|
||||||
// Filter out elements whose ids are not in clientIds
|
|
||||||
const filteredData = jsonData.filter(
|
|
||||||
(item) => !clientIds.includes(item.id)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Send the filtered data back to the client
|
|
||||||
connection.socket.send(JSON.stringify(filteredData));
|
|
||||||
} else {
|
} else {
|
||||||
console.error("File not found:", filePath);
|
console.error("File not found:", filePath);
|
||||||
clearInterval(sendInterval);
|
clearInterval(sendInterval);
|
||||||
@ -356,21 +341,6 @@ fastify.register(async function (fastify) {
|
|||||||
// Start sending data periodically
|
// Start sending data periodically
|
||||||
sendInterval = setInterval(sendData, 5000);
|
sendInterval = setInterval(sendData, 5000);
|
||||||
|
|
||||||
// Handle incoming messages from the client to update the ids
|
|
||||||
connection.socket.on("message", (message) => {
|
|
||||||
try {
|
|
||||||
const parsedMessage = JSON.parse(message);
|
|
||||||
if (parsedMessage?.ids) {
|
|
||||||
//console.log("Received ids from client:", parsedMessage.ids);
|
|
||||||
clientIds = parsedMessage.ids; // Update the ids list from the client
|
|
||||||
} else {
|
|
||||||
//console.log("No ids received in the message");
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Error parsing incoming message:", error);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Handle client disconnect
|
// Handle client disconnect
|
||||||
connection.socket.on("close", () => {
|
connection.socket.on("close", () => {
|
||||||
console.log("Client disconnected");
|
console.log("Client disconnected");
|
||||||
@ -393,7 +363,66 @@ 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() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user