This commit is contained in:
hailin 2025-06-15 20:52:52 +08:00
parent 870e77efdf
commit 012e266a09
1 changed files with 11 additions and 13 deletions

View File

@ -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;
}