test sse fixes
This commit is contained in:
@@ -37,13 +37,22 @@ export const Route = createFileRoute("/api/events/$")({
|
||||
clearInterval(pingInterval);
|
||||
return;
|
||||
}
|
||||
const pingMessage = `data: ${JSON.stringify({ type: "ping" })}\n\n`;
|
||||
const pingMessage = `data: ${JSON.stringify({ type: "ping", timestamp: Date.now() })}\n\n`;
|
||||
controller.enqueue(new TextEncoder().encode(pingMessage));
|
||||
} catch (e) {
|
||||
logger.error("ServerEvents | Ping interval error", e);
|
||||
clearInterval(pingInterval);
|
||||
}
|
||||
}, 30000);
|
||||
}, 15000);
|
||||
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const heartbeatMessage = `data: ${JSON.stringify({ type: "heartbeat", timestamp: Date.now() })}\n\n`;
|
||||
controller.enqueue(new TextEncoder().encode(heartbeatMessage));
|
||||
} catch (e) {
|
||||
logger.error("ServerEvents | Heartbeat error", e);
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
const cleanup = () => {
|
||||
serverEvents.off("test", handleEvent);
|
||||
@@ -63,6 +72,9 @@ export const Route = createFileRoute("/api/events/$")({
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Headers": "Cache-Control",
|
||||
"X-Accel-Buffering": "no",
|
||||
"X-Proxy-Buffering": "no",
|
||||
"Proxy-Buffering": "off",
|
||||
"Transfer-Encoding": "chunked",
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
@@ -19,6 +19,7 @@ const eventHandlers: Record<string, EventHandler> = {
|
||||
logger.info("New Connection");
|
||||
},
|
||||
"ping": () => {},
|
||||
"heartbeat": () => {},
|
||||
"match": (event, queryClient) => {
|
||||
queryClient.invalidateQueries(tournamentQueries.details(event.tournamentId))
|
||||
queryClient.invalidateQueries(tournamentQueries.current())
|
||||
@@ -73,15 +74,15 @@ export function useServerEvents() {
|
||||
logger.error("SSE connection error", error);
|
||||
eventSource.close();
|
||||
|
||||
if (shouldConnectRef.current && retryCountRef.current < 5) {
|
||||
if (shouldConnectRef.current && retryCountRef.current < 10) {
|
||||
retryCountRef.current += 1;
|
||||
const delay = Math.min(
|
||||
1000 * Math.pow(2, retryCountRef.current - 1),
|
||||
30000
|
||||
1000 * Math.pow(1.5, retryCountRef.current - 1),
|
||||
15000
|
||||
);
|
||||
|
||||
logger.info(
|
||||
`SSE reconnection attempt ${retryCountRef.current}/5 in ${delay}ms`
|
||||
`SSE reconnection attempt ${retryCountRef.current}/10 in ${delay}ms`
|
||||
);
|
||||
|
||||
timeoutRef.current = setTimeout(() => {
|
||||
@@ -89,7 +90,7 @@ export function useServerEvents() {
|
||||
connectEventSource();
|
||||
}
|
||||
}, delay);
|
||||
} else if (retryCountRef.current >= 5) {
|
||||
} else if (retryCountRef.current >= 10) {
|
||||
logger.error("SSE max reconnection attempts reached");
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user