regularly ping acceleration websocket server

This commit is contained in:
Mononaut 2024-11-11 16:26:30 +00:00
parent 937e82bb89
commit 9718610104
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -49,6 +49,7 @@ class AccelerationApi {
private websocketPath = config.MEMPOOL_SERVICES?.API ? `${config.MEMPOOL_SERVICES.API.replace('https://', 'wss://').replace('http://', 'ws://')}/accelerator/ws` : '/';
private _accelerations: Record<string, Acceleration> = {};
private lastPoll = 0;
private lastPing = 0;
private forcePoll = false;
private myAccelerations: Record<string, { status: MyAccelerationStatus, added: number, acceleration?: Acceleration }> = {};
@ -267,12 +268,27 @@ class AccelerationApi {
this.ws.on('message', (data, isBinary) => {
try {
const parsedMsg = JSON.parse((isBinary ? data : data.toString()) as string);
const msg = (isBinary ? data : data.toString()) as string;
const parsedMsg = msg?.length ? JSON.parse(msg) : null;
this.handleWebsocketMessage(parsedMsg);
} catch (e) {
logger.warn('Failed to parse acceleration websocket message: ' + (e instanceof Error ? e.message : e));
}
});
this.ws.on('ping', () => {
logger.debug('received ping from acceleration websocket server');
});
this.ws.on('pong', () => {
logger.debug('received pong from acceleration websocket server');
});
} else {
if (Date.now() - this.lastPing > 30000) {
logger.debug('sending ping to acceleration websocket server');
this.ws.ping();
this.lastPing = Date.now();
}
}
await new Promise(resolve => setTimeout(resolve, 5000));
}