Skip to content

Commit

Permalink
Merge pull request #31 from make-software/fix-ping-reconnect
Browse files Browse the repository at this point in the history
fix: Fix ping reconnect for event handler
  • Loading branch information
Volodymyr-Kuchinskyi authored Nov 21, 2024
2 parents eea7f18 + d1f5286 commit 77d1481
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Config {
lotteryContractPackageHash: string;
dbURI: string;
clientURL: string[];
pingCheckIntervalInMilliseconds: number;
}

export const config: Config = {
Expand All @@ -21,4 +22,5 @@ export const config: Config = {
lotteryContractPackageHash: process.env.LOTTERY_CONTRACT_PACKAGE_HASH as string,
dbURI: process.env.DB_URI as string,
clientURL: process.env.CLIENT_URL ? (process.env.CLIENT_URL as string).split(',') : [],
pingCheckIntervalInMilliseconds: 60000,
};
13 changes: 13 additions & 0 deletions server/src/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,22 @@ async function main() {
console.log(`Connected to streaming API: ${config.csprCloudStreamingUrl}`);
})

let lastPingTimestamp = new Date();

setInterval(() => {
const now = new Date();
if (now.getTime() - lastPingTimestamp.getTime() > config.pingCheckIntervalInMilliseconds) {
console.log(`No ping events from Streaming API for ${config.pingCheckIntervalInMilliseconds/1000} seconds, closing ws connection...`);
ws.close();
process.exit(1);
}
}, config.pingCheckIntervalInMilliseconds);

ws.on('message', async (data: Buffer) => {
const rawData = data.toString();

if (rawData === 'Ping') {
lastPingTimestamp = new Date();
return;
}

Expand Down

0 comments on commit 77d1481

Please sign in to comment.