29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
/**
|
||
* 将 HTTP API 根地址转为 WebSocket 根(用于 /android/scrcpy/ws)。
|
||
* next dev 下若未设置 NEXT_PUBLIC_API_URL,浏览器会连到 :3000,WebSocket 升级通常无法被中间件代理;
|
||
* 真流调试请设 NEXT_PUBLIC_API_URL 指向 uvicorn(例如 http://127.0.0.1:8001)。
|
||
*/
|
||
|
||
export function httpApiBaseToWsBase(apiBase: string): string {
|
||
if (typeof window === "undefined") return "ws://127.0.0.1:8001";
|
||
const base = (apiBase || "").trim();
|
||
if (base) {
|
||
try {
|
||
const u = new URL(base, window.location.href);
|
||
const proto = u.protocol === "https:" ? "wss:" : "ws:";
|
||
return `${proto}//${u.host}`;
|
||
} catch {
|
||
/* ignore */
|
||
}
|
||
}
|
||
const { protocol, host } = window.location;
|
||
const proto = protocol === "https:" ? "wss:" : "ws:";
|
||
return `${proto}//${host}`;
|
||
}
|
||
|
||
export function androidScrcpyWsUrl(apiBase: string, serial: string, maxSize = 1920): string {
|
||
const root = httpApiBaseToWsBase(apiBase).replace(/\/$/, "");
|
||
const q = new URLSearchParams({ serial, max_size: String(maxSize) });
|
||
return `${root}/android/scrcpy/ws?${q.toString()}`;
|
||
}
|