update websocket
This commit is contained in:
parent
407592f002
commit
d3d68cb1e6
120
fastify/app.js
120
fastify/app.js
@ -177,67 +177,83 @@ fastify.register(async function (fastify) {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
fastify.register(async function (fastify) {
|
fastify.register(async function (fastify) {
|
||||||
fastify.get(
|
fastify.get("/options-flow-reader", { websocket: true }, (connection, req) => {
|
||||||
"/options-flow-reader",
|
let jsonData;
|
||||||
{ websocket: true },
|
let sendInterval;
|
||||||
(connection, req) => {
|
let lastSentData = [];
|
||||||
let jsonData;
|
|
||||||
let sendInterval;
|
|
||||||
|
|
||||||
// Function to send 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, "../app/json/options-flow/feed/data.json");
|
||||||
__dirname,
|
|
||||||
"../app/json/options-flow/feed/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);
|
|
||||||
connection?.socket?.close();
|
|
||||||
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
|
try {
|
||||||
sendData();
|
if (fs.existsSync(filePath)) {
|
||||||
|
const fileData = fs.readFileSync(filePath, "utf8").trim();
|
||||||
|
|
||||||
// Start sending data periodically
|
if (!fileData) {
|
||||||
sendInterval = setInterval(sendData, 1000);
|
console.error("File is empty:", filePath);
|
||||||
|
setTimeout(sendData, 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Handle client disconnect
|
let parsedData;
|
||||||
connection.socket.on("close", () => {
|
try {
|
||||||
console.log("Client disconnected");
|
parsedData = JSON.parse(fileData);
|
||||||
clearInterval(sendInterval);
|
} catch (jsonErr) {
|
||||||
});
|
console.error("Invalid JSON format:", jsonErr);
|
||||||
|
setTimeout(sendData, 2000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Handle server crash cleanup
|
// Send data only if the length has increased since the last send
|
||||||
const closeHandler = () => {
|
if (parsedData.length > lastSentData.length) {
|
||||||
console.log("Server is closing. Cleaning up resources...");
|
connection.socket.send(JSON.stringify(parsedData));
|
||||||
clearInterval(sendInterval);
|
console.log("Options data sent: Length", parsedData.length);
|
||||||
connection.socket.close();
|
lastSentData = parsedData;
|
||||||
};
|
}
|
||||||
|
} else {
|
||||||
// Add close handler to process event
|
console.error("File not found:", filePath);
|
||||||
process.on("exit", closeHandler);
|
setTimeout(sendData, 2000);
|
||||||
process.on("SIGINT", closeHandler);
|
|
||||||
process.on("SIGTERM", closeHandler);
|
|
||||||
process.on("uncaughtException", closeHandler);
|
|
||||||
process.on("unhandledRejection", closeHandler);
|
|
||||||
}
|
}
|
||||||
);
|
} catch (err) {
|
||||||
});
|
console.error("Error sending data to client:", err);
|
||||||
|
setTimeout(sendData, 2000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Send data to the client initially
|
||||||
|
sendData();
|
||||||
|
|
||||||
|
// Start sending data periodically
|
||||||
|
sendInterval = setInterval(sendData, 500);
|
||||||
|
|
||||||
|
// Handle client disconnect
|
||||||
|
connection.socket.on("close", () => {
|
||||||
|
console.log("Client disconnected");
|
||||||
|
clearInterval(sendInterval);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle server crash cleanup
|
||||||
|
const closeHandler = () => {
|
||||||
|
console.log("Server is closing. Cleaning up resources...");
|
||||||
|
clearInterval(sendInterval);
|
||||||
|
if (connection.socket.readyState === connection.socket.OPEN) {
|
||||||
|
connection.socket.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add close handler to process events
|
||||||
|
process.on("exit", closeHandler);
|
||||||
|
process.on("SIGINT", closeHandler);
|
||||||
|
process.on("SIGTERM", closeHandler);
|
||||||
|
process.on("uncaughtException", closeHandler);
|
||||||
|
process.on("unhandledRejection", closeHandler);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
fastify.register(async function (fastify) {
|
fastify.register(async function (fastify) {
|
||||||
fastify.get(
|
fastify.get(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user