Files
gitlab-instance-0a899031_do…/web-console/lib/androidScrcpyWs.ts
2026-03-29 02:57:08 -05:00

29 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 将 HTTP API 根地址转为 WebSocket 根(用于 /android/scrcpy/ws
* next dev 下若未设置 NEXT_PUBLIC_API_URL浏览器会连到 :3000WebSocket 升级通常无法被中间件代理;
* 真流调试请设 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()}`;
}