From 50557b9230b2f7fdf50712967ccb8efa9f0542c9 Mon Sep 17 00:00:00 2001 From: KQL Date: Wed, 24 Sep 2025 15:12:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=AB=AF=E5=8F=A3=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=A3=80=E6=B5=8B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改进状态检测算法,使用多种方法组合 - 添加调试工具 check-status.html - 处理CORS限制问题 - 使用script、image和XMLHttpRequest多种检测方式 --- check-status.html | 248 ++++++++++++++++++++++++++++++++++++++++++++++ index.html | 103 ++++++++++++++++--- 2 files changed, 336 insertions(+), 15 deletions(-) create mode 100644 check-status.html diff --git a/check-status.html b/check-status.html new file mode 100644 index 0000000..612b052 --- /dev/null +++ b/check-status.html @@ -0,0 +1,248 @@ + + + + + + 产业状态检查 - 调试版 + + + +
+

🔍 产业状态检查工具

+ +
+ + + +
+ 检查日志:
+
+
+ + + + \ No newline at end of file diff --git a/index.html b/index.html index df23fb2..201ac95 100644 --- a/index.html +++ b/index.html @@ -501,25 +501,98 @@ // 检查单个产业状态 async function checkStatus(port) { const statusEl = document.getElementById(`status-${port}`); + + // 使用多种方法检测端口状态 try { - // 尝试访问端口 - const response = await fetch(`http://localhost:${port}`, { - mode: 'no-cors', - cache: 'no-cache' - }); + // 方法1: 创建一个script标签来测试 + const testScript = document.createElement('script'); + testScript.src = `http://localhost:${port}/vite.svg?t=${Date.now()}`; + testScript.onerror = () => { + // 方法2: 使用Image对象检测 + const img = new Image(); + let isResolved = false; - // 使用图片加载来检测 - const img = new Image(); - img.src = `http://localhost:${port}/favicon.ico?t=${Date.now()}`; + const checkTimeout = setTimeout(() => { + if (!isResolved) { + isResolved = true; + statusEl.className = 'status status-stopped'; + statusEl.innerHTML = ' 已停止'; + } + }, 2000); - await new Promise((resolve, reject) => { - img.onload = resolve; - img.onerror = reject; - setTimeout(reject, 2000); - }); + img.onload = () => { + if (!isResolved) { + isResolved = true; + clearTimeout(checkTimeout); + statusEl.className = 'status status-running'; + statusEl.innerHTML = ' 运行中'; + } + }; + + img.onerror = () => { + // 方法3: 尝试使用XMLHttpRequest + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', `http://localhost:${port}/`, true); + xhr.timeout = 2000; + + xhr.onreadystatechange = function() { + if (!isResolved && xhr.readyState === 4) { + isResolved = true; + clearTimeout(checkTimeout); + // 即使有CORS错误,如果收到响应就说明服务在运行 + if (xhr.status === 0 || xhr.status === 200) { + // status为0通常表示CORS阻止,但服务器确实响应了 + statusEl.className = 'status status-running'; + statusEl.innerHTML = ' 运行中'; + } else { + statusEl.className = 'status status-stopped'; + statusEl.innerHTML = ' 已停止'; + } + } + }; + + xhr.onerror = function() { + if (!isResolved) { + isResolved = true; + clearTimeout(checkTimeout); + // 网络错误通常意味着服务未运行 + statusEl.className = 'status status-stopped'; + statusEl.innerHTML = ' 已停止'; + } + }; + + try { + xhr.send(); + } catch(e) { + // 发送失败,但这可能是因为CORS + if (!isResolved) { + isResolved = true; + clearTimeout(checkTimeout); + statusEl.className = 'status status-running'; + statusEl.innerHTML = ' 运行中'; + } + } + }; + + // 尝试加载各种可能的静态资源 + const possiblePaths = [ + '/favicon.ico', + '/vite.svg', + '/index.html', + '/assets/index.css' + ]; + + img.src = `http://localhost:${port}${possiblePaths[0]}?t=${Date.now()}`; + }; + + testScript.onload = () => { + statusEl.className = 'status status-running'; + statusEl.innerHTML = ' 运行中'; + }; + + document.head.appendChild(testScript); + setTimeout(() => document.head.removeChild(testScript), 100); - statusEl.className = 'status status-running'; - statusEl.innerHTML = ' 运行中'; } catch (error) { statusEl.className = 'status status-stopped'; statusEl.innerHTML = ' 已停止';