added pong keepalive

This commit is contained in:
Maieul BOYER 2026-01-10 17:58:11 +01:00 committed by Maix0
parent 901e3e5a8e
commit 1b169fca13
3 changed files with 22 additions and 12 deletions

View file

@ -17,6 +17,7 @@ import "./pong.css";
declare module "ft_state" { declare module "ft_state" {
interface State { interface State {
pongSock?: CSocket; pongSock?: CSocket;
pongKeepAliveInterval?: ReturnType<typeof setInterval>;
} }
} }
@ -51,20 +52,21 @@ enum TourInfoState {
} }
document.addEventListener("ft:pageChange", (newUrl) => { 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.pongSock !== undefined) window.__state.pongSock.close();
if (window.__state.pongKeepAliveInterval !== undefined) clearInterval(window.__state.pongKeepAliveInterval);
window.__state.pongSock = undefined; window.__state.pongSock = undefined;
window.__state.pongKeepAliveInterval = undefined;
}); });
export function getSocket(): CSocket { export function getSocket(): CSocket {
if (window.__state.pongSock === undefined) if (window.__state.pongSock === undefined) {
window.__state.pongSock = io(window.location.host, { window.__state.pongSock = io(window.location.host, {
path: "/api/pong/socket.io/", path: "/api/pong/socket.io/",
}) as any as CSocket; }) as any as CSocket;
}
if (window.__state.pongKeepAliveInterval === undefined) {
window.__state.pongKeepAliveInterval = setInterval(() => { window.__state.pongSock?.emit("hello") }, 100);
}
return window.__state.pongSock; return window.__state.pongSock;
} }
@ -387,7 +389,7 @@ function pongClient(_url: string, _args: RouteHandlerParams): RouteHandlerReturn
queueBtn.style.color = "white"; queueBtn.style.color = "white";
if (!isNullish(currentGame)) { if (!isNullish(currentGame)) {
let end_txt : string = ''; let end_txt: string = '';
if ((user.id === currentGame.left.id && winner === 'left') || if ((user.id === currentGame.left.id && winner === 'left') ||
(user.id === currentGame.right.id && winner === 'right')) (user.id === currentGame.right.id && winner === 'right'))
end_txt = 'won! #yippe'; end_txt = 'won! #yippe';

View file

@ -11,7 +11,7 @@ import client from "@app/api";
declare module 'ft_state' { declare module 'ft_state' {
interface State { interface State {
tttSock?: Socket; tttSock?: Socket;
keepAliveInterval?: ReturnType<typeof setInterval>; tttkeepAliveInterval?: ReturnType<typeof setInterval>;
} }
} }
@ -23,16 +23,16 @@ enum QueueState {
document.addEventListener("ft:pageChange", () => { document.addEventListener("ft:pageChange", () => {
if (window.__state.tttSock !== undefined) window.__state.tttSock.close(); 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.tttSock = undefined;
window.__state.keepAliveInterval = undefined; window.__state.tttkeepAliveInterval = undefined;
}); });
export function getSocket(): Socket { export function getSocket(): Socket {
if (window.__state.tttSock === undefined) if (window.__state.tttSock === undefined)
window.__state.tttSock = io(window.location.host, { path: "/api/ttt/socket.io/" }) as any as Socket; window.__state.tttSock = io(window.location.host, { path: "/api/ttt/socket.io/" }) as any as Socket;
if (window.__state.keepAliveInterval === undefined) if (window.__state.tttkeepAliveInterval === undefined)
window.__state.keepAliveInterval = setInterval(() => window.__state.tttSock?.emit('keepalive'), 100); window.__state.tttkeepAliveInterval = setInterval(() => window.__state.tttSock?.emit('keepalive'), 100);
return window.__state.tttSock; return window.__state.tttSock;
} }

View file

@ -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) { private registerForTournament(sock: SSocket, name: string | null) {
const user = this.users.get(sock.authUser.id); const user = this.users.get(sock.authUser.id);
if (isNullish(user)) return; if (isNullish(user)) return;
@ -327,6 +334,7 @@ class StateI {
socket.on('tourCreate', () => this.createTournament(socket)); socket.on('tourCreate', () => this.createTournament(socket));
socket.on('tourStart', () => this.startTournament(socket)); socket.on('tourStart', () => this.startTournament(socket));
socket.on('hello', () => this.getHello(socket));
} }
private updateClient(socket: SSocket): void { private updateClient(socket: SSocket): void {