30 lines
1.3 KiB
Plaintext
30 lines
1.3 KiB
Plaintext
|
|
The file /home/project/src/hooks/useNetworkStatus.ts has been updated. Made 1 replacement.
|
|||
|
|
|
|||
|
|
Here's the result of running `cat -n` on a snippet of the edited file:
|
|||
|
|
50→
|
|||
|
|
51→ window.addEventListener('online', handleOnline);
|
|||
|
|
52→ window.addEventListener('offline', handleOffline);
|
|||
|
|
53→
|
|||
|
|
54→ // 检测慢网络(使用轻量级 DNS 预解析代替 HTTP 请求)
|
|||
|
|
55→ const checkConnectionSpeed = async () => {
|
|||
|
|
56→ if (!navigator.onLine) return;
|
|||
|
|
57→
|
|||
|
|
58→ // 仅通过 navigator.connection API 检测(如果可用)
|
|||
|
|
59→ const connection = (navigator as any).connection;
|
|||
|
|
60→ if (connection) {
|
|||
|
|
61→ setState(prev => ({
|
|||
|
|
62→ ...prev,
|
|||
|
|
63→ isSlowConnection: connection.effectiveType === 'slow-2g' || connection.effectiveType === '2g'
|
|||
|
|
64→ }));
|
|||
|
|
65→ }
|
|||
|
|
66→ // 如果不支持 Connection API,则不进行主动探测,避免产生大量 502 错误
|
|||
|
|
67→ };
|
|||
|
|
68→
|
|||
|
|
69→ // 每60秒检测一次网络质量(降低频率)
|
|||
|
|
70→ const interval = setInterval(checkConnectionSpeed, 60000);
|
|||
|
|
71→ checkConnectionSpeed();
|
|||
|
|
72→
|
|||
|
|
73→ return () => {
|
|||
|
|
74→ window.removeEventListener('online', handleOnline);
|
|||
|
|
75→ window.removeEventListener('offline', handleOffline);
|
|||
|
|
76→ clearInterval(interval);
|