修复端口状态检测问题
- 改进状态检测算法,使用多种方法组合 - 添加调试工具 check-status.html - 处理CORS限制问题 - 使用script、image和XMLHttpRequest多种检测方式
This commit is contained in:
103
index.html
103
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 = '<span class="status-dot"></span> 已停止';
|
||||
}
|
||||
}, 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 = '<span class="status-dot"></span> 运行中';
|
||||
}
|
||||
};
|
||||
|
||||
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 = '<span class="status-dot"></span> 运行中';
|
||||
} else {
|
||||
statusEl.className = 'status status-stopped';
|
||||
statusEl.innerHTML = '<span class="status-dot"></span> 已停止';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = function() {
|
||||
if (!isResolved) {
|
||||
isResolved = true;
|
||||
clearTimeout(checkTimeout);
|
||||
// 网络错误通常意味着服务未运行
|
||||
statusEl.className = 'status status-stopped';
|
||||
statusEl.innerHTML = '<span class="status-dot"></span> 已停止';
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
xhr.send();
|
||||
} catch(e) {
|
||||
// 发送失败,但这可能是因为CORS
|
||||
if (!isResolved) {
|
||||
isResolved = true;
|
||||
clearTimeout(checkTimeout);
|
||||
statusEl.className = 'status status-running';
|
||||
statusEl.innerHTML = '<span class="status-dot"></span> 运行中';
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 尝试加载各种可能的静态资源
|
||||
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 = '<span class="status-dot"></span> 运行中';
|
||||
};
|
||||
|
||||
document.head.appendChild(testScript);
|
||||
setTimeout(() => document.head.removeChild(testScript), 100);
|
||||
|
||||
statusEl.className = 'status status-running';
|
||||
statusEl.innerHTML = '<span class="status-dot"></span> 运行中';
|
||||
} catch (error) {
|
||||
statusEl.className = 'status status-stopped';
|
||||
statusEl.innerHTML = '<span class="status-dot"></span> 已停止';
|
||||
|
||||
Reference in New Issue
Block a user