diff --git a/src/utils/getWsBaseUrl.ts b/src/utils/getWsBaseUrl.ts new file mode 100644 index 0000000..331b78a --- /dev/null +++ b/src/utils/getWsBaseUrl.ts @@ -0,0 +1,16 @@ +/** + * Origin for Pond WebSocket URLs. + * + * VITE_APP_API_URL usually ends with /v1 (same base axios uses for REST). WS paths are + * written as /v1/live/..., so we strip a trailing /v1 from the env URL to avoid /v1/v1/. + */ +export function getWsBaseUrl(): string { + const apiUrl = import.meta.env.VITE_APP_API_URL; + if (apiUrl) { + let ws = apiUrl.replace(/^http/, "ws"); + ws = ws.replace(/\/v1\/?$/, ""); + return ws.replace(/\/$/, "") || ws; + } + const protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; + return `${protocol}//${window.location.host}`; +}