refactor(sse): improve error handling and connection cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// GET /api/rooms/[code]/stream — SSE : pousse l'état de la room en temps réel
|
||||
// GET /api/rooms/[code]/stream - SSE : pousse l'état de la room en temps réel
|
||||
// Le client s'abonne une seule fois ; chaque saveRoom publie sur Redis et notifie ici.
|
||||
|
||||
import { NextRequest } from "next/server";
|
||||
@@ -47,7 +47,11 @@ export async function GET(
|
||||
|
||||
sub.on("error", () => {
|
||||
clearInterval(keepAlive);
|
||||
try { controller.close(); } catch { /* déjà fermé */ }
|
||||
try {
|
||||
controller.close();
|
||||
} catch {
|
||||
/* déjà fermé */
|
||||
}
|
||||
sub.disconnect();
|
||||
});
|
||||
},
|
||||
|
||||
+16
-5
@@ -97,25 +97,36 @@ export function useMultiGame() {
|
||||
es.onmessage = (e) => {
|
||||
try {
|
||||
setRoom(JSON.parse(e.data) as Room);
|
||||
} catch { /* ignore */ }
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
};
|
||||
|
||||
es.onerror = () => {
|
||||
// EventSource reconnecte automatiquement — pas besoin de gérer manuellement
|
||||
// EventSource reconnecte automatiquement - pas besoin de gérer manuellement
|
||||
};
|
||||
}
|
||||
|
||||
// Quand le tab redevient visible : heartbeat immédiat pour rafraîchir l'état
|
||||
useEffect(() => {
|
||||
function onVisible() {
|
||||
if (document.visibilityState === "visible" && sseCodeRef.current && ssePidRef.current) {
|
||||
if (
|
||||
document.visibilityState === "visible" &&
|
||||
sseCodeRef.current &&
|
||||
ssePidRef.current
|
||||
) {
|
||||
fetch(`/api/rooms/${sseCodeRef.current}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "heartbeat", playerId: ssePidRef.current }),
|
||||
body: JSON.stringify({
|
||||
action: "heartbeat",
|
||||
playerId: ssePidRef.current,
|
||||
}),
|
||||
})
|
||||
.then((r) => r.json())
|
||||
.then((d) => { if ((d as { room: Room }).room) setRoom((d as { room: Room }).room); })
|
||||
.then((d) => {
|
||||
if ((d as { room: Room }).room) setRoom((d as { room: Room }).room);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user