From b92655cb3cae4202e2f04b57732554b2640fd3d2 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 8 Oct 2024 15:58:56 +0200 Subject: [PATCH] update websocket to include bid/ask price --- fastify/app.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/fastify/app.js b/fastify/app.js index 6f553d7..a84ca6f 100755 --- a/fastify/app.js +++ b/fastify/app.js @@ -172,17 +172,25 @@ fastify.register(async function (fastify) { try { const jsonData = JSON.parse(stringData); - const bpData = jsonData.bp; - - // Check if bpData is a number and not equal to zero - if (typeof bpData === "number" && bpData !== 0) { - if (connection.socket.readyState === WebSocket.OPEN && !isSend) { - connection.socket.send(JSON.stringify({ bp: bpData })); - isSend = true; - setTimeout(() => { - isSend = false; - }, 2000); - } + // Check if bpData is a number, not equal to zero, and jsonData properties are not null/undefined + if ( + jsonData?.bp != null && + jsonData?.ap != null && + jsonData?.lp != null && + connection.socket.readyState === WebSocket.OPEN && + !isSend + ) { + connection.socket.send( + JSON.stringify({ + bp: jsonData.bp?.toFixed(2), + ap: jsonData.ap?.toFixed(2), + lp: jsonData.lp?.toFixed(2), + }) + ); + isSend = true; + setTimeout(() => { + isSend = false; + }, 2000); } } catch (error) { console.error("Error parsing JSON:", error);