Files
jiang13-bbs/frontend/components/RealtimeProvider.tsx

20 lines
630 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
import { useEffect } from "react";
import { realtime } from "@/lib/realtime";
// 实时总线生命周期:登录即连、登出即断;连接/重连/心跳全部封装在 lib/realtime。
// 挂在持久 root layout 上,全标签页只维护一条 WebSocket。
export default function RealtimeProvider({ userId }: { userId: number | null }) {
useEffect(() => {
if (userId) {
realtime.connect();
// 用 release 而非立刻 disconnect:避开 Strict Mode / HMR 瞬时卸载打断握手
return () => realtime.release();
}
realtime.disconnect();
}, [userId]);
return null;
}