From 831e72c6919222cf9c463cb59efe32abd7e69a6c Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Mon, 8 Sep 2025 05:11:42 +0200 Subject: [PATCH] add forEachClient --- src/services/wsService.ts | 81 ++++++++++++--------------------------- 1 file changed, 25 insertions(+), 56 deletions(-) diff --git a/src/services/wsService.ts b/src/services/wsService.ts index b7f7efb1..4404eb9a 100644 --- a/src/services/wsService.ts +++ b/src/services/wsService.ts @@ -192,83 +192,52 @@ const wsOnConnect = (ws: WebSocket, req: http.IncomingMessage): void => { }); }; -export const sendWsBroadcast = (data: IWsMsgToClient): void => { - const msg = JSON.stringify(data); +const forEachClient = (cb: (client: IWsCustomData) => void): void => { if (wsServer) { for (const client of wsServer.clients) { - client.send(msg); + cb(client as IWsCustomData); } } if (wssServer) { for (const client of wssServer.clients) { - client.send(msg); + cb(client as IWsCustomData); } } }; +export const sendWsBroadcast = (data: IWsMsgToClient): void => { + const msg = JSON.stringify(data); + forEachClient(client => { + client.send(msg); + }); +}; + export const sendWsBroadcastTo = (accountId: string, data: IWsMsgToClient): void => { const msg = JSON.stringify(data); - if (wsServer) { - for (const client of wsServer.clients) { - if ((client as IWsCustomData).accountId == accountId) { - client.send(msg); - } + forEachClient(client => { + if (client.accountId == accountId) { + client.send(msg); } - } - if (wssServer) { - for (const client of wssServer.clients) { - if ((client as IWsCustomData).accountId == accountId) { - client.send(msg); - } - } - } + }); }; export const sendWsBroadcastEx = (data: IWsMsgToClient, accountId?: string, excludeWsid?: number): void => { const msg = JSON.stringify(data); - if (wsServer) { - for (const client of wsServer.clients) { - if ( - (!accountId || (client as IWsCustomData).accountId == accountId) && - (client as IWsCustomData).id != excludeWsid - ) { - client.send(msg); - } + forEachClient(client => { + if ((!accountId || client.accountId == accountId) && client.id != excludeWsid) { + client.send(msg); } - } - if (wssServer) { - for (const client of wssServer.clients) { - if ( - (!accountId || (client as IWsCustomData).accountId == accountId) && - (client as IWsCustomData).id != excludeWsid - ) { - client.send(msg); - } - } - } + }); }; export const handleNonceInvalidation = (accountId: string): void => { - if (wsServer) { - for (const client of wsServer.clients) { - if ((client as IWsCustomData).accountId == accountId) { - if ((client as IWsCustomData).isGame) { - client.close(); - } else { - client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient)); - } + forEachClient(client => { + if (client.accountId == accountId) { + if (client.isGame) { + client.close(); + } else { + client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient)); } } - } - if (wssServer) { - for (const client of wssServer.clients) { - if ((client as IWsCustomData).accountId == accountId) { - if ((client as IWsCustomData).isGame) { - client.close(); - } else { - client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient)); - } - } - } - } + }); };