|
| 1 | +import type { ManagerOptions, SocketOptions } from 'socket.io-client' |
| 2 | +import { tryUseNuxtApp, useAppConfig, useRuntimeConfig } from '#app' |
| 3 | + |
| 4 | +/** WebSocket d'abord ; Socket.IO repasse en long-polling si le proxy bloque l'upgrade. */ |
| 5 | +export const SOCKET_IO_TRANSPORTS = ['websocket', 'polling'] as const |
| 6 | + |
| 7 | +export function buildSocketIoClientOptions(auth: { id: string; key: string }): Partial<ManagerOptions & SocketOptions> { |
| 8 | + return { |
| 9 | + path: '/socket.io', |
| 10 | + query: { id: String(auth.id), key: String(auth.key) }, |
| 11 | + transports: [...SOCKET_IO_TRANSPORTS], |
| 12 | + reconnectionAttempts: 10, |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +type PublicRuntimeConfig = ReturnType<typeof useRuntimeConfig>['public'] |
| 17 | + |
| 18 | +function readPublicRuntimeConfig(): PublicRuntimeConfig { |
| 19 | + const nuxtApp = tryUseNuxtApp() |
| 20 | + if (nuxtApp?.$config?.public) { |
| 21 | + return nuxtApp.$config.public as PublicRuntimeConfig |
| 22 | + } |
| 23 | + |
| 24 | + if (import.meta.client) { |
| 25 | + const injected = (window as { __NUXT__?: { config?: { public?: PublicRuntimeConfig } } }).__NUXT__?.config?.public |
| 26 | + if (injected) { |
| 27 | + return injected |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return useRuntimeConfig().public |
| 32 | +} |
| 33 | + |
| 34 | +function resolveBrowserApiPort(publicConfig: PublicRuntimeConfig): number { |
| 35 | + const publicPort = Number(publicConfig.socketPublicApiPort) |
| 36 | + if (publicPort > 0) { |
| 37 | + return publicPort |
| 38 | + } |
| 39 | + |
| 40 | + const internalPort = Number(publicConfig.socketApiPort) |
| 41 | + if (internalPort > 0) { |
| 42 | + return internalPort |
| 43 | + } |
| 44 | + |
| 45 | + return 4000 |
| 46 | +} |
| 47 | + |
| 48 | +function resolveDirectApiOrigin(hostname: string, publicConfig: PublicRuntimeConfig): string { |
| 49 | + const protocol = `${publicConfig.socketApiProtocol || 'http:'}` |
| 50 | + const port = resolveBrowserApiPort(publicConfig) |
| 51 | + return `${protocol}//${hostname}:${port}` |
| 52 | +} |
| 53 | + |
1 | 54 | /** |
2 | 55 | * Origine Socket.IO côté navigateur. |
3 | | - * Par défaut : même origine que le front (Nitro/Vite proxifient `/socket.io` vers l'API). |
4 | | - * Si `SESAME_APP_PUBLIC_API_URL` est défini : connexion directe à l'API (requis quand le |
5 | | - * reverse-proxy route `/socket.io` vers le port 4000 ou que le proxy Nitro n'est pas joignable). |
| 56 | + * |
| 57 | + * Priorité : |
| 58 | + * 1. `SESAME_APP_PUBLIC_API_URL` si défini (prod / reverse-proxy). |
| 59 | + * 2. En dev, si le front et l'API publique sont sur des ports différents : connexion directe |
| 60 | + * (ex. front `mactacx:3002` → API `mactacx:4002` via `SESAME_APP_PUBLIC_API_PORT`). |
| 61 | + * 3. Sinon : même origine que le front (Nitro proxifie `/socket.io` en long-polling). |
| 62 | + * |
| 63 | + * Note : `SESAME_APP_API_URL` (port interne, ex. 4000 dans Docker) sert au proxy serveur Nuxt, |
| 64 | + * pas à l'origine Socket.IO du navigateur. |
6 | 65 | */ |
7 | 66 | export function resolveSocketApiOrigin(): string { |
8 | | - const runtimeConfig = useRuntimeConfig() |
9 | | - const configuredPublic = `${runtimeConfig.public.socketApiUrl || ''}`.trim() |
| 67 | + const publicConfig = readPublicRuntimeConfig() |
| 68 | + const configuredPublic = `${publicConfig.socketApiUrl || ''}`.trim() |
10 | 69 |
|
11 | 70 | if (configuredPublic) { |
12 | 71 | return new URL(configuredPublic).origin |
13 | 72 | } |
14 | 73 |
|
15 | 74 | if (import.meta.client) { |
| 75 | + const apiPort = resolveBrowserApiPort(publicConfig) |
| 76 | + const frontPort = Number(window.location.port) || (window.location.protocol === 'https:' ? 443 : 80) |
| 77 | + |
| 78 | + if (import.meta.dev && frontPort !== apiPort) { |
| 79 | + return resolveDirectApiOrigin(window.location.hostname, publicConfig) |
| 80 | + } |
| 81 | + |
16 | 82 | return window.location.origin |
17 | 83 | } |
18 | 84 |
|
|
0 commit comments