feat: 用 SSE 替代 SWR 轮询,实现房间状态实时推送

SSE 断连时自动降级为 2s 轮询,重连后切回 SSE。
This commit is contained in:
2026-02-24 19:51:30 +08:00
parent f6949a062f
commit 8c0d89af6d
9 changed files with 223 additions and 71 deletions
+76
View File
@@ -0,0 +1,76 @@
import { buildRoomStatus } from "@/lib/buildRoomStatus";
import { subscribe } from "@/lib/roomEvents";
export const dynamic = "force-dynamic";
export async function GET(
req: Request,
{ params }: { params: Promise<{ id: string }> },
) {
const { id } = await params;
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
const send = (obj: object) => {
try {
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(obj)}\n\n`),
);
} catch {
/* controller already closed */
}
};
let alive = true;
(async () => {
const status = await buildRoomStatus(id);
if (!status) {
send({ error: "room_not_found" });
controller.close();
return;
}
if (alive) send(status);
})();
const unsubscribe = subscribe(id, async () => {
if (!alive) return;
try {
const status = await buildRoomStatus(id);
if (status && alive) send(status);
} catch {
/* ignore transient read errors */
}
});
const heartbeat = setInterval(() => {
try {
controller.enqueue(encoder.encode(": heartbeat\n\n"));
} catch {
clearInterval(heartbeat);
}
}, 30_000);
req.signal.addEventListener("abort", () => {
alive = false;
unsubscribe();
clearInterval(heartbeat);
try {
controller.close();
} catch {
/* already closed */
}
});
},
});
return new Response(stream, {
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache, no-transform",
Connection: "keep-alive",
},
});
}
+3
View File
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { atomicUpdateRoom } from "@/lib/store";
import { notify } from "@/lib/roomEvents";
export async function POST(
req: Request,
@@ -27,6 +28,8 @@ export async function POST(
);
}
notify(id);
return NextResponse.json({
roomId: id,
userCount: updated.users.length,
+3
View File
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { atomicUpdateRoom } from "@/lib/store";
import { notify } from "@/lib/roomEvents";
export async function POST(
req: Request,
@@ -36,6 +37,8 @@ export async function POST(
);
}
notify(id);
return NextResponse.json({ ok: true });
} catch (e) {
console.error("Failed to reset room:", e);
+4 -59
View File
@@ -1,6 +1,5 @@
import { NextResponse } from "next/server";
import { getRoomData } from "@/lib/store";
import type { MatchType } from "@/types";
import { buildRoomStatus } from "@/lib/buildRoomStatus";
export async function GET(
_req: Request,
@@ -9,60 +8,16 @@ export async function GET(
const { id } = await params;
try {
const data = await getRoomData(id);
const status = await buildRoomStatus(id);
if (!data) {
if (!status) {
return NextResponse.json(
{ error: "房间不存在或已过期" },
{ status: 404 },
);
}
const total = data.restaurants.length;
const allFinished =
data.users.length > 0 &&
data.users.every((u) => (data.swipeCounts[u] ?? 0) >= total);
let match = data.match;
let matchType: MatchType = null;
let matchLikes = 0;
let runnerUps: { id: string; likes: number }[] = [];
if (match) {
matchType = "unanimous";
matchLikes = data.users.length;
} else if (allFinished && data.restaurants.length > 0) {
const ranked = rankRestaurants(data.likes, data.restaurants);
if (ranked[0].likes > 0) {
match = ranked[0].id;
matchType = "best";
matchLikes = ranked[0].likes;
runnerUps = ranked.slice(1, 3).filter((r) => r.likes > 0);
} else {
match = ranked[0].id;
matchType = "no_match";
matchLikes = 0;
}
}
const likeCounts: Record<string, number> = {};
for (const [rid, users] of Object.entries(data.likes)) {
if (users.length > 0) {
likeCounts[rid] = users.length;
}
}
return NextResponse.json({
roomId: id,
userCount: data.users.length,
match,
matchType,
matchLikes,
runnerUps,
likeCounts,
swipeCounts: data.swipeCounts,
restaurants: data.restaurants,
});
return NextResponse.json(status);
} catch (e) {
console.error("Failed to get room:", e);
return NextResponse.json(
@@ -71,13 +26,3 @@ export async function GET(
);
}
}
function rankRestaurants(
likes: Record<string, string[]>,
restaurants: { id: string; rating: number }[],
): { id: string; likes: number }[] {
return restaurants
.map((r) => ({ id: r.id, likes: likes[r.id]?.length ?? 0, rating: r.rating }))
.sort((a, b) => b.likes - a.likes || b.rating - a.rating)
.map(({ id, likes: l }) => ({ id, likes: l }));
}
+3
View File
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { atomicUpdateRoom } from "@/lib/store";
import { notify } from "@/lib/roomEvents";
export async function POST(
req: Request,
@@ -52,6 +53,8 @@ export async function POST(
);
}
notify(id);
return NextResponse.json({
match: updated.match,
likeCount: updated.likes[rid]?.length ?? 0,
+3
View File
@@ -1,5 +1,6 @@
import { NextResponse } from "next/server";
import { atomicUpdateRoom } from "@/lib/store";
import { notify } from "@/lib/roomEvents";
export async function POST(
req: Request,
@@ -46,6 +47,8 @@ export async function POST(
);
}
notify(id);
return NextResponse.json({ ok: true });
} catch (e) {
console.error("Failed to undo swipe:", e);