Skip to content

Commit

Permalink
adding pong and clear timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwin24 committed Jul 31, 2024
1 parent 0895f1a commit b819994
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,35 @@ function sendRequest(ws: WebSocket) {
ws.send(JSON.stringify(request));
}

let ws: WebSocket;
let pingInterval: string | number | NodeJS.Timeout | undefined;
let pongTimeout: string | number | NodeJS.Timeout | undefined;

function startPing(ws: WebSocket) {
setInterval(() => {
pingInterval = setInterval(() => {
if (ws.readyState === WebSocket.OPEN) {
console.log("Sending ping...");
ws.ping(undefined, undefined, (err) => {
if (err) {
console.log("Failed to send ping:", err);
}
});
} else {
console.log("WebSocket is not open");
ws.ping();
console.log("Ping sent");

pongTimeout = setTimeout(() => {
console.error("Pong not received in time, closing connection");
ws.terminate();
}, 5000);
}
}, 15000);
}, 30000);
}

function cleanupWebSocket() {
clearInterval(pingInterval);
clearTimeout(pongTimeout);
if (ws) {
ws.terminate();
}
}

function initializeWebSocket() {
console.log("Initializing WebSocket...");
const ws = new WebSocket(
`wss://atlas-mainnet.helius-rpc.com/?api-key=${apiKey}`
);
ws = new WebSocket(`wss://atlas-mainnet.helius-rpc.com/?api-key=${apiKey}`);

ws.on("open", function open() {
console.log("WebSocket is open");
Expand All @@ -75,7 +84,6 @@ function initializeWebSocket() {
});

ws.on("message", async function incoming(data) {
// console.log("Received message");
const messageStr = data.toString("utf8");
try {
const messageObj = JSON.parse(messageStr);
Expand All @@ -88,13 +96,20 @@ function initializeWebSocket() {
}
});

ws.on("pong", function pong() {
console.log("Pong received");
clearTimeout(pongTimeout);
});

ws.on("error", function error(err) {
console.log("WebSocket error:", err);
cleanupWebSocket();
});

ws.on("close", function close() {
console.log("WebSocket is closed, attempting to restart...");
setTimeout(initializeWebSocket, 5000); // Restart after 5 second
console.log("WebSocket is closed");
cleanupWebSocket();
setTimeout(initializeWebSocket, 5000);
});
}

Expand All @@ -105,6 +120,20 @@ export const messageQueues: {
} = {};
export const messageTimestamps: { [key: number]: Array<number> } = {};

const cleanUpInactiveGroups = () => {
const now = Date.now();
Object.keys(messageTimestamps).forEach(groupId => {
const parsedGroupId = Number(groupId);

if (messageTimestamps[parsedGroupId].every(timestamp => now - timestamp > 60000)) {
delete messageQueues[parsedGroupId];
delete messageTimestamps[parsedGroupId];
}
});
};

setInterval(cleanUpInactiveGroups, 600000);

const sendQueuedMessages = async (groupId: number) => {
const now = Date.now();

Expand All @@ -126,10 +155,10 @@ const sendQueuedMessages = async (groupId: number) => {

try {
console.log(`Sending message to group ${groupId}`);
await bot.telegram.sendPhoto(groupId, message.image, {
caption: message.caption,
parse_mode: "Markdown",
});
// await bot.telegram.sendPhoto(groupId, message.image, {
// caption: message.caption,
// parse_mode: "Markdown",
// });
messages.shift();
messageTimestamps[groupId].push(now);
} catch (error) {
Expand Down

0 comments on commit b819994

Please sign in to comment.