Spaces:
Runtime error
Runtime error
Download frontend/src/lib/socket.ts from mistral-hackaton-2026/ChatCraft: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/mistral-hackaton-2026/ChatCraft/resolve/main/frontend/src/lib/socket.ts
- Command line
-
hf download hf://spaces/mistral-hackaton-2026/ChatCraft/frontend/src/lib/socket.ts
-
curl -L -o socket.ts https://huggingface.co/spaces/mistral-hackaton-2026/ChatCraft/resolve/main/frontend/src/lib/socket.ts
1.23 kB
| import { io, type Socket } from 'socket.io-client'; | |
| let _socket: Socket | null = null; | |
| export function backendUrl(): string { | |
| if (typeof window === 'undefined') return 'http://localhost:8000'; | |
| // VITE_BACKEND_URL overrides (e.g. for dev: http://localhost:8000). | |
| // In dev, use backend origin so /static and /sprites are served correctly (proxy can return HTML otherwise). | |
| if (import.meta.env.VITE_BACKEND_URL) return import.meta.env.VITE_BACKEND_URL; | |
| const origin = window.location.origin; | |
| if (origin === 'http://localhost:5173' || origin === 'http://127.0.0.1:5173') return 'http://localhost:8000'; | |
| return origin; | |
| } | |
| export function getSocket(): Socket { | |
| if (!_socket) { | |
| _socket = io(backendUrl(), { | |
| transports: ['websocket', 'polling'], | |
| reconnectionAttempts: 5, | |
| reconnectionDelay: 1500, | |
| }); | |
| _socket.on('connect', () => console.log('[socket] connected', _socket?.id)); | |
| _socket.on('disconnect', (reason) => console.log('[socket] disconnected', reason)); | |
| _socket.on('connect_error', (err) => console.error('[socket] error', err.message)); | |
| } | |
| return _socket; | |
| } | |
| export function disconnectSocket(): void { | |
| if (_socket) { | |
| _socket.disconnect(); | |
| _socket = null; | |
| } | |
| } | |