update websocket

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

View File

@ -116,15 +116,24 @@ fastify.register(async function (fastify) {
connection.socket.readyState === WebSocket.OPEN && connection.socket.readyState === WebSocket.OPEN &&
!isSend !isSend
) { ) {
// Calculate the average price
const avgPrice =
(parseFloat(jsonData.ap) +
parseFloat(jsonData.bp) +
parseFloat(jsonData.lp)) /
3;
connection.socket.send( connection.socket.send(
JSON.stringify({ JSON.stringify({
bp: jsonData?.bp, bp: jsonData?.bp,
ap: jsonData?.ap, ap: jsonData?.ap,
lp: jsonData?.lp?.toFixed(2), lp: jsonData?.lp?.toFixed(2),
avgPrice: avgPrice?.toFixed(2), // Add the computed average price
type: jsonData?.type, type: jsonData?.type,
time: formatTimestampNewYork(jsonData?.t), time: formatTimestampNewYork(jsonData?.t),
}) })
); );
isSend = true; isSend = true;
setTimeout(() => { setTimeout(() => {
isSend = false; isSend = false;
@ -141,7 +150,8 @@ fastify.register(async function (fastify) {
clearInterval(sendInterval); clearInterval(sendInterval);
connection.socket.close(); 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) => {
@ -359,7 +369,6 @@ fastify.register(async function (fastify) {
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,
@ -370,6 +379,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");
const jsonData = JSON?.parse(fileData); const jsonData = JSON?.parse(fileData);
// Only send data if conditions are met and data has changed // Only send data if conditions are met and data has changed
if ( if (
jsonData?.lp != null && jsonData?.lp != null &&
@ -379,6 +389,12 @@ fastify.register(async function (fastify) {
["Q", "T"].includes(jsonData?.type) && ["Q", "T"].includes(jsonData?.type) &&
connection.socket.readyState === WebSocket.OPEN connection.socket.readyState === WebSocket.OPEN
) { ) {
// Calculate the average price
const avgPrice =
((parseFloat(jsonData.ap) +
parseFloat(jsonData.bp) +
parseFloat(jsonData.lp)) /
3);
// Check if the current data is different from the last sent data // Check if the current data is different from the last sent data
const currentDataSignature = `${jsonData?.lp}`; const currentDataSignature = `${jsonData?.lp}`;
@ -389,6 +405,9 @@ fastify.register(async function (fastify) {
dataToSend?.push({ dataToSend?.push({
symbol, // Include the ticker symbol in the sent data symbol, // Include the ticker symbol in the sent data
ap: jsonData?.ap, ap: jsonData?.ap,
bp: jsonData?.bp,
lp: jsonData?.lp,
avgPrice: avgPrice, // Add the computed average price
}); });
// Update the last sent data for this ticker // Update the last sent data for this ticker
@ -406,9 +425,10 @@ fastify.register(async function (fastify) {
// Send all collected data as a single message // Send all collected data as a single message
if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) { if (dataToSend.length > 0 && connection.socket.readyState === WebSocket.OPEN) {
connection.socket.send(JSON.stringify(dataToSend)); connection.socket.send(JSON.stringify(dataToSend));
//console.log(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) => {