diff --git a/apps/blogai/components/header.tsx b/apps/blogai/components/header.tsx index a3f9b01..0fc7d7d 100644 --- a/apps/blogai/components/header.tsx +++ b/apps/blogai/components/header.tsx @@ -182,16 +182,14 @@ export async function getWsBase() { let wsProtocol = "ws"; - if ( - typeof window !== "undefined" && - window.location && - window.location.protocol === "https:" - ) { - // ✅ 浏览器环境下 - wsProtocol = "wss"; - ip = window.location.hostname; + if (typeof window !== "undefined") { + // ✅ 客户端环境(浏览器执行) + if (window.location.protocol === "https:") { + wsProtocol = "wss"; + ip = window.location.hostname; // ✅ 用 hostname 避免 HTTPS + IP 的 TLS 报错 + } } else { - // ✅ 服务端环境,延迟导入 headers + // ✅ SSR 环境,延迟导入 headers(不能放顶层) const { headers } = await import("next/headers"); const hdrs = headers(); @@ -203,14 +201,14 @@ export async function getWsBase() { } if (hostHeader) { + // ✅ host 可能带端口,需处理 ip = hostHeader.includes(":") ? hostHeader.split(":")[0] : hostHeader; } } - - const finalUrl = `${wsProtocol}://${ip}/api/v1/deploy/ws`; - console.log(".........[WebSocket] Final URL:", finalUrl); - return `${wsProtocol}://${ip}/api/v1/deploy/ws`; + const finalUrl = `${wsProtocol}://${ip}/api/v1/deploy/ws`; + console.log("✅ [WebSocket] Final URL:", finalUrl); + return finalUrl; }