| 185 | // ── WebSocket ──────────────────────────────────────────────────────────────── |
| 186 | |
| 187 | function getWsUrl(): string { |
| 188 | const proto = location.protocol === 'https:' ? 'wss:' : 'ws:' |
| 189 | const url = new URL(`${proto}//${location.host}/ws`) |
| 190 | |
| 191 | // Auth token: URL param wins, then localStorage |
| 192 | const params = new URLSearchParams(location.search) |
| 193 | const token = params.get('token') ?? localStorage.getItem('claude-terminal-token') |
| 194 | if (token) { |
| 195 | url.searchParams.set('token', token) |
| 196 | localStorage.setItem('claude-terminal-token', token) |
| 197 | } |
| 198 | |
| 199 | // Pass current terminal dimensions so the PTY is spawned at the right size |
| 200 | url.searchParams.set('cols', String(term.cols)) |
| 201 | url.searchParams.set('rows', String(term.rows)) |
| 202 | |
| 203 | return url.toString() |
| 204 | } |
| 205 | |
| 206 | function sendJSON(msg: Record<string, unknown>): void { |
| 207 | if (ws?.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg)) |