From 1b169fca13855a68d1b12a6924fc056116a14c46 Mon Sep 17 00:00:00 2001 From: Maieul BOYER Date: Sat, 10 Jan 2026 17:58:11 +0100 Subject: [PATCH] added pong keepalive --- frontend/src/pages/pong/pong.ts | 16 +++++++++------- frontend/src/pages/ttt/ttt.ts | 10 +++++----- src/pong/src/state.ts | 8 ++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/frontend/src/pages/pong/pong.ts b/frontend/src/pages/pong/pong.ts index 60a9aab..7bf44fb 100644 --- a/frontend/src/pages/pong/pong.ts +++ b/frontend/src/pages/pong/pong.ts @@ -17,6 +17,7 @@ import "./pong.css"; declare module "ft_state" { interface State { pongSock?: CSocket; + pongKeepAliveInterval?: ReturnType; } } @@ -51,20 +52,21 @@ enum TourInfoState { } document.addEventListener("ft:pageChange", (newUrl) => { - if ( - newUrl.detail.startsWith("/app/pong") || - newUrl.detail.startsWith("/pong") - ) - return; if (window.__state.pongSock !== undefined) window.__state.pongSock.close(); + if (window.__state.pongKeepAliveInterval !== undefined) clearInterval(window.__state.pongKeepAliveInterval); window.__state.pongSock = undefined; + window.__state.pongKeepAliveInterval = undefined; }); export function getSocket(): CSocket { - if (window.__state.pongSock === undefined) + if (window.__state.pongSock === undefined) { window.__state.pongSock = io(window.location.host, { path: "/api/pong/socket.io/", }) as any as CSocket; + } + if (window.__state.pongKeepAliveInterval === undefined) { + window.__state.pongKeepAliveInterval = setInterval(() => { window.__state.pongSock?.emit("hello") }, 100); + } return window.__state.pongSock; } @@ -387,7 +389,7 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn queueBtn.style.color = "white"; if (!isNullish(currentGame)) { - let end_txt : string = ''; + let end_txt: string = ''; if ((user.id === currentGame.left.id && winner === 'left') || (user.id === currentGame.right.id && winner === 'right')) end_txt = 'won! #yippe'; diff --git a/frontend/src/pages/ttt/ttt.ts b/frontend/src/pages/ttt/ttt.ts index d0a0fdc..71f9e5b 100644 --- a/frontend/src/pages/ttt/ttt.ts +++ b/frontend/src/pages/ttt/ttt.ts @@ -11,7 +11,7 @@ import client from "@app/api"; declare module 'ft_state' { interface State { tttSock?: Socket; - keepAliveInterval?: ReturnType; + tttkeepAliveInterval?: ReturnType; } } @@ -23,16 +23,16 @@ enum QueueState { document.addEventListener("ft:pageChange", () => { if (window.__state.tttSock !== undefined) window.__state.tttSock.close(); - if (window.__state.keepAliveInterval !== undefined) clearInterval(window.__state.keepAliveInterval); + if (window.__state.tttkeepAliveInterval !== undefined) clearInterval(window.__state.tttkeepAliveInterval); window.__state.tttSock = undefined; - window.__state.keepAliveInterval = undefined; + window.__state.tttkeepAliveInterval = undefined; }); export function getSocket(): Socket { if (window.__state.tttSock === undefined) window.__state.tttSock = io(window.location.host, { path: "/api/ttt/socket.io/" }) as any as Socket; - if (window.__state.keepAliveInterval === undefined) - window.__state.keepAliveInterval = setInterval(() => window.__state.tttSock?.emit('keepalive'), 100); + if (window.__state.tttkeepAliveInterval === undefined) + window.__state.tttkeepAliveInterval = setInterval(() => window.__state.tttSock?.emit('keepalive'), 100); return window.__state.tttSock; } diff --git a/src/pong/src/state.ts b/src/pong/src/state.ts index 64db4b5..17b39a2 100644 --- a/src/pong/src/state.ts +++ b/src/pong/src/state.ts @@ -48,6 +48,13 @@ class StateI { }; } + private getHello(socket: SSocket) { + const user = this.users.get(socket.authUser.id); + if (isNullish(user)) return; + + user.lastSeen = Date.now(); + } + private registerForTournament(sock: SSocket, name: string | null) { const user = this.users.get(sock.authUser.id); if (isNullish(user)) return; @@ -327,6 +334,7 @@ class StateI { socket.on('tourCreate', () => this.createTournament(socket)); socket.on('tourStart', () => this.startTournament(socket)); + socket.on('hello', () => this.getHello(socket)); } private updateClient(socket: SSocket): void {