update websocket

This commit is contained in:
MuslemRahimi 2024-12-02 21:41:15 +01:00
parent 4b8d3d52ef
commit 62d49193bd

View File

@ -99,49 +99,59 @@ fastify.register(async function (fastify) {
// Function to send data to the client // Function to send data to the client
const sendData = async () => { const sendData = async () => {
if (!symbol) return; // Check if symbol is defined if (!symbol) return; // Check if symbol is defined
const filePath = path.join(__dirname, `../app/json/websocket/companies/${symbol}.json`); const filePath = path.join(__dirname, `../app/json/websocket/companies/${symbol}.json`);
try { try {
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);
// Logic to send data if certain conditions are met
if (
jsonData?.lp != null &&
jsonData?.t != null &&
["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN &&
!isSend
) {
// Calculate the average price
const avgPrice =
(parseFloat(jsonData.ap) +
parseFloat(jsonData.bp) +
parseFloat(jsonData.lp)) /
3;
connection.socket.send(
JSON.stringify({
bp: jsonData?.bp,
ap: jsonData?.ap,
lp: jsonData?.lp?.toFixed(2),
avgPrice: avgPrice?.toFixed(2), // Add the computed average price
type: jsonData?.type,
time: formatTimestampNewYork(jsonData?.t),
})
);
isSend = true;
setTimeout(() => {
isSend = false;
}, 500); // Reset isSend after 500ms
}
} else {
console.error("File not found:", filePath);
clearInterval(sendInterval);
connection.socket.close();
console.error("Connection closed");
}
} catch (err) {
console.error("Error sending data to client:", err);
clearInterval(sendInterval);
connection.socket.close();
}
};
// Logic to send data if certain conditions are met
if (
jsonData?.lp != null &&
jsonData?.t != null &&
["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN &&
!isSend
) {
connection.socket.send(
JSON.stringify({
bp: jsonData?.bp,
ap: jsonData?.ap,
lp: jsonData?.lp?.toFixed(2),
type: jsonData?.type,
time: formatTimestampNewYork(jsonData?.t),
})
);
isSend = true;
setTimeout(() => {
isSend = false;
}, 500); // Reset isSend after 500ms
}
} else {
console.error("File not found:", filePath);
clearInterval(sendInterval);
connection.socket.close();
console.error("Connection closed");
}
} catch (err) {
console.error("Error sending data to client:", err);
clearInterval(sendInterval);
connection.socket.close();
}
};
// Start receiving messages from the client // Start receiving messages from the client
connection.socket.on("message", (message) => { connection.socket.on("message", (message) => {
@ -356,59 +366,69 @@ fastify.register(async function (fastify) {
const lastSentData = {}; const lastSentData = {};
// Function to send data for all tickers as a list // Function to send data for all tickers as a list
const sendData = async () => { const sendData = async () => {
const dataToSend = []; const dataToSend = [];
// Iterate over tickers and collect data for (const symbol of tickers) {
for (const symbol of tickers) { const filePath = path?.join(
const filePath = path?.join( __dirname,
__dirname, `../app/json/websocket/companies/${symbol}.json`
`../app/json/websocket/companies/${symbol}.json` );
);
try { try {
if (fs?.existsSync(filePath)) { if (fs?.existsSync(filePath)) {
const fileData = fs?.readFileSync(filePath, "utf8"); const fileData = fs?.readFileSync(filePath, "utf8");
const jsonData = JSON?.parse(fileData); const jsonData = JSON?.parse(fileData);
// Only send data if conditions are met and data has changed
if (
jsonData?.lp != null &&
jsonData?.ap != null &&
jsonData?.bp != null &&
jsonData?.t != null &&
["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN
) {
// Check if the current data is different from the last sent data // Only send data if conditions are met and data has changed
const currentDataSignature = `${jsonData?.lp}`; if (
const lastSentSignature = lastSentData[symbol]; jsonData?.lp != null &&
jsonData?.ap != null &&
jsonData?.bp != null &&
jsonData?.t != null &&
["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN
) {
// Calculate the average price
const avgPrice =
((parseFloat(jsonData.ap) +
parseFloat(jsonData.bp) +
parseFloat(jsonData.lp)) /
3);
if (currentDataSignature !== lastSentSignature) { // Check if the current data is different from the last sent data
// Collect data to send const currentDataSignature = `${jsonData?.lp}`;
dataToSend?.push({ const lastSentSignature = lastSentData[symbol];
symbol, // Include the ticker symbol in the sent data
ap: jsonData?.ap,
});
// Update the last sent data for this ticker if (currentDataSignature !== lastSentSignature) {
lastSentData[symbol] = currentDataSignature; // Collect data to send
} dataToSend?.push({
} symbol, // Include the ticker symbol in the sent data
} else { ap: jsonData?.ap,
//console.error("File not found for ticker:", symbol); bp: jsonData?.bp,
} lp: jsonData?.lp,
} catch (err) { avgPrice: avgPrice, // Add the computed average price
console.error("Error processing data for ticker:", symbol, err); });
// Update the last sent data for this ticker
lastSentData[symbol] = currentDataSignature;
} }
} }
} else {
//console.error("File not found for ticker:", symbol);
}
} catch (err) {
console.error("Error processing data for ticker:", symbol, err);
}
}
// Send all collected data as a single message
if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) {
connection.socket.send(JSON.stringify(dataToSend));
//console.log(dataToSend);
}
};
// Send all collected data as a single message
if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) {
connection.socket.send(JSON.stringify(dataToSend));
//console.log(dataToSend)
}
};
// Start receiving messages from the client // Start receiving messages from the client
connection.socket.on("message", (message) => { connection.socket.on("message", (message) => {