update websocket
This commit is contained in:
parent
4b8d3d52ef
commit
62d49193bd
@ -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;
|
||||||
@ -143,6 +152,7 @@ fastify.register(async function (fastify) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Start receiving messages from the client
|
// Start receiving messages from the client
|
||||||
connection.socket.on("message", (message) => {
|
connection.socket.on("message", (message) => {
|
||||||
symbol = message.toString("utf-8")?.toUpperCase();
|
symbol = message.toString("utf-8")?.toUpperCase();
|
||||||
@ -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,10 +425,11 @@ 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) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user