added pong keepalive
This commit is contained in:
parent
901e3e5a8e
commit
1b169fca13
3 changed files with 22 additions and 12 deletions
|
|
@ -17,6 +17,7 @@ import "./pong.css";
|
|||
declare module "ft_state" {
|
||||
interface State {
|
||||
pongSock?: CSocket;
|
||||
pongKeepAliveInterval?: ReturnType<typeof setInterval>;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import client from "@app/api";
|
|||
declare module 'ft_state' {
|
||||
interface State {
|
||||
tttSock?: Socket;
|
||||
keepAliveInterval?: ReturnType<typeof setInterval>;
|
||||
tttkeepAliveInterval?: ReturnType<typeof setInterval>;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue