From 1ace32e5c391d6cf9bf9d216e1d6e3d79ac38ab3 Mon Sep 17 00:00:00 2001 From: Steve Korshakov Date: Wed, 16 Jul 2025 22:37:28 -0700 Subject: [PATCH] feat: add ping --- sources/app/api.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/sources/app/api.ts b/sources/app/api.ts index 0e97df4..f8fea23 100644 --- a/sources/app/api.ts +++ b/sources/app/api.ts @@ -426,7 +426,7 @@ export async function startApi() { methodsToRemove.push(method); } } - + if (methodsToRemove.length > 0) { log({ module: 'websocket-rpc' }, `Cleaning up RPC methods on disconnect for socket ${socket.id}: ${methodsToRemove.join(', ')}`); methodsToRemove.forEach(method => userRpcMap.delete(method)); @@ -829,7 +829,7 @@ export async function startApi() { if (userRpcMap && userRpcMap.get(method) === socket) { userRpcMap.delete(method); log({ module: 'websocket-rpc' }, `RPC method unregistered: ${method} from socket ${socket.id} (user: ${userId})`); - + if (userRpcMap.size === 0) { rpcListeners.delete(userId); log({ module: 'websocket-rpc' }, `All RPC methods unregistered for user ${userId}`); @@ -920,7 +920,7 @@ export async function startApi() { const duration = Date.now() - startTime; const errorMsg = error instanceof Error ? error.message : 'RPC call failed'; log({ module: 'websocket-rpc' }, `RPC call failed: ${method} - ${errorMsg} (${duration}ms)`); - + // Timeout or error occurred if (callback) { callback({ @@ -931,6 +931,10 @@ export async function startApi() { } }); + socket.on('ping', async (callback: (response: any) => void) => { + callback({}); + }); + socket.emit('auth', { success: true, user: userId }); log({ module: 'websocket' }, `User connected: ${userId}`); });