diff --git a/web/src/lib/api/client.ts b/web/src/lib/api/client.ts index 091abcc..48b0a4a 100644 --- a/web/src/lib/api/client.ts +++ b/web/src/lib/api/client.ts @@ -27,8 +27,11 @@ export function mihomoUrl(alias: string, path: string): string { */ export function mihomoWsUrl(alias: string, path: string): string { const p = path.replace(/^\/+/, ''); - const base = gatewayBase(); - const wsBase = base.replace(/^http/, 'ws'); + const base = + gatewayBase() || (typeof window !== 'undefined' ? window.location.origin.replace(/\/$/, '') : ''); + let wsBase = base; + if (wsBase.startsWith('https://')) wsBase = `wss://${wsBase.slice('https://'.length)}`; + else if (wsBase.startsWith('http://')) wsBase = `ws://${wsBase.slice('http://'.length)}`; return `${wsBase}/api/${encodeURIComponent(alias)}/mihomo/${p}`; } diff --git a/web/src/routes/servers/[alias]/mihomo/+page.svelte b/web/src/routes/servers/[alias]/mihomo/+page.svelte index 86086ed..1cd7839 100644 --- a/web/src/routes/servers/[alias]/mihomo/+page.svelte +++ b/web/src/routes/servers/[alias]/mihomo/+page.svelte @@ -190,6 +190,7 @@ const reader = res.body.getReader(); const decoder = new TextDecoder(); let buf = ''; + let seen = 0; try { while (!signal.aborted) { const { done, value } = await reader.read(); @@ -198,17 +199,21 @@ const nl = buf.indexOf('\n'); if (nl < 0) continue; const line = buf.slice(0, nl).trim(); + buf = buf.slice(nl + 1); if (line) { try { const o = JSON.parse(line) as { inuse?: number }; memKB = Number(o.inuse) || 0; histMem = [...histMem, memKB]; while (histMem.length > MAX_POINTS) histMem.shift(); + seen++; } catch { /* ignore */ } } - break; + // В Mihomo для /memory первая строка нередко 0 после нового подключения. + // Берём хотя бы две строки в рамках одного запроса, затем завершаем. + if (seen >= 2) break; } } finally { try {