feat: support websocket connections from game client #2735

Merged
Sainan merged 19 commits from client-ws into main 2025-09-10 00:00:10 -07:00
Showing only changes of commit 831e72c691 - Show all commits

View File

@ -192,83 +192,52 @@ const wsOnConnect = (ws: WebSocket, req: http.IncomingMessage): void => {
}); });
}; };
export const sendWsBroadcast = (data: IWsMsgToClient): void => { const forEachClient = (cb: (client: IWsCustomData) => void): void => {
const msg = JSON.stringify(data);
if (wsServer) { if (wsServer) {
for (const client of wsServer.clients) { for (const client of wsServer.clients) {
client.send(msg); cb(client as IWsCustomData);
} }
} }
if (wssServer) { if (wssServer) {
for (const client of wssServer.clients) { for (const client of wssServer.clients) {
cb(client as IWsCustomData);
}
}
};
export const sendWsBroadcast = (data: IWsMsgToClient): void => {
const msg = JSON.stringify(data);
forEachClient(client => {
client.send(msg); client.send(msg);
} });
}
}; };
export const sendWsBroadcastTo = (accountId: string, data: IWsMsgToClient): void => { export const sendWsBroadcastTo = (accountId: string, data: IWsMsgToClient): void => {
const msg = JSON.stringify(data); const msg = JSON.stringify(data);
if (wsServer) { forEachClient(client => {
for (const client of wsServer.clients) { if (client.accountId == accountId) {
if ((client as IWsCustomData).accountId == accountId) {
client.send(msg); 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 => { export const sendWsBroadcastEx = (data: IWsMsgToClient, accountId?: string, excludeWsid?: number): void => {
const msg = JSON.stringify(data); const msg = JSON.stringify(data);
if (wsServer) { forEachClient(client => {
for (const client of wsServer.clients) { if ((!accountId || client.accountId == accountId) && client.id != excludeWsid) {
if (
(!accountId || (client as IWsCustomData).accountId == accountId) &&
(client as IWsCustomData).id != excludeWsid
) {
client.send(msg); 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 => { export const handleNonceInvalidation = (accountId: string): void => {
if (wsServer) { forEachClient(client => {
for (const client of wsServer.clients) { if (client.accountId == accountId) {
if ((client as IWsCustomData).accountId == accountId) { if (client.isGame) {
if ((client as IWsCustomData).isGame) {
client.close(); client.close();
} else { } else {
client.send(JSON.stringify({ nonce_updated: true } satisfies IWsMsgToClient)); 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));
}
}
}
}
}; };