MCPcopy Create free account
hub / github.com/code100x/chess / useSocket

Function useSocket

apps/frontend/src/hooks/useSocket.ts:6–28  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4const WS_URL = import.meta.env.VITE_APP_WS_URL ?? 'ws://localhost:8080';
5
6export const useSocket = () => {
7 const [socket, setSocket] = useState<WebSocket | null>(null);
8 const user = useUser();
9
10 useEffect(() => {
11 if (!user) return;
12 const ws = new WebSocket(`${WS_URL}?token=${user.token}`);
13
14 ws.onopen = () => {
15 setSocket(ws);
16 };
17
18 ws.onclose = () => {
19 setSocket(null);
20 };
21
22 return () => {
23 ws.close();
24 };
25 }, [user]);
26
27 return socket;
28};

Callers 1

GameFunction · 0.90

Calls 1

useUserFunction · 0.90

Tested by

no test coverage detected