File size: 746 Bytes
842ca4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | export class NetworkMonitor {
static async checkNetworkQuality(): Promise<number> {
// 简单的网络质量检测
const startTime = performance.now()
try {
const response = await fetch('/api/health-check', {
method: 'HEAD',
cache: 'no-cache'
})
const endTime = performance.now()
return endTime - startTime
} catch (error) {
return -1
}
}
static getNetworkInfo() {
const connection = (navigator as any).connection
return connection ? {
effectiveType: connection.effectiveType,
downlink: connection.downlink,
rtt: connection.rtt,
saveData: connection.saveData
} : null
}
static isOnline(): boolean {
return navigator.onLine
}
} |