Refactor Mihomo WebSocket URL handling and improve Svelte data processing
- Updated the `mihomoWsUrl` function to handle cases where the gateway base URL may be undefined, ensuring robust WebSocket URL generation. - Enhanced the Svelte component to read and process memory metrics more reliably by implementing a mechanism to capture multiple lines of data before concluding the read operation, improving data accuracy.
This commit is contained in:
@@ -27,8 +27,11 @@ export function mihomoUrl(alias: string, path: string): string {
|
|||||||
*/
|
*/
|
||||||
export function mihomoWsUrl(alias: string, path: string): string {
|
export function mihomoWsUrl(alias: string, path: string): string {
|
||||||
const p = path.replace(/^\/+/, '');
|
const p = path.replace(/^\/+/, '');
|
||||||
const base = gatewayBase();
|
const base =
|
||||||
const wsBase = base.replace(/^http/, 'ws');
|
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}`;
|
return `${wsBase}/api/${encodeURIComponent(alias)}/mihomo/${p}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -190,6 +190,7 @@
|
|||||||
const reader = res.body.getReader();
|
const reader = res.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let buf = '';
|
let buf = '';
|
||||||
|
let seen = 0;
|
||||||
try {
|
try {
|
||||||
while (!signal.aborted) {
|
while (!signal.aborted) {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
@@ -198,17 +199,21 @@
|
|||||||
const nl = buf.indexOf('\n');
|
const nl = buf.indexOf('\n');
|
||||||
if (nl < 0) continue;
|
if (nl < 0) continue;
|
||||||
const line = buf.slice(0, nl).trim();
|
const line = buf.slice(0, nl).trim();
|
||||||
|
buf = buf.slice(nl + 1);
|
||||||
if (line) {
|
if (line) {
|
||||||
try {
|
try {
|
||||||
const o = JSON.parse(line) as { inuse?: number };
|
const o = JSON.parse(line) as { inuse?: number };
|
||||||
memKB = Number(o.inuse) || 0;
|
memKB = Number(o.inuse) || 0;
|
||||||
histMem = [...histMem, memKB];
|
histMem = [...histMem, memKB];
|
||||||
while (histMem.length > MAX_POINTS) histMem.shift();
|
while (histMem.length > MAX_POINTS) histMem.shift();
|
||||||
|
seen++;
|
||||||
} catch {
|
} catch {
|
||||||
/* ignore */
|
/* ignore */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
// В Mihomo для /memory первая строка нередко 0 после нового подключения.
|
||||||
|
// Берём хотя бы две строки в рамках одного запроса, затем завершаем.
|
||||||
|
if (seen >= 2) break;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user