()
| 998 | |
| 999 | // Optimized measurePing function with better error handling |
| 1000 | async function measurePing() { |
| 1001 | if (!isConnected || !win) return; |
| 1002 | |
| 1003 | try { |
| 1004 | const startTime = Date.now(); |
| 1005 | // Using axios with optimized settings |
| 1006 | await axios.get('https://www.gstatic.com/generate_204', { |
| 1007 | timeout: 5000, // Reduced timeout for faster response |
| 1008 | validateStatus: () => true, // Accept any status code |
| 1009 | headers: { |
| 1010 | 'Cache-Control': 'no-cache', |
| 1011 | 'Pragma': 'no-cache' |
| 1012 | } |
| 1013 | }); |
| 1014 | const endTime = Date.now(); |
| 1015 | const pingTime = endTime - startTime; |
| 1016 | |
| 1017 | // Send ping to renderer only if there's a significant change or first measurement |
| 1018 | if (!global.lastPing || Math.abs(global.lastPing - pingTime) > 5) { |
| 1019 | global.lastPing = pingTime; |
| 1020 | win.webContents.send('connection-stats', { |
| 1021 | ping: pingTime, |
| 1022 | download: null, |
| 1023 | upload: null |
| 1024 | }); |
| 1025 | } |
| 1026 | } catch (error) { |
| 1027 | console.error('Ping error:', error); |
| 1028 | // Send error ping to renderer only if state changed |
| 1029 | if (global.lastPing !== -1) { |
| 1030 | global.lastPing = -1; |
| 1031 | win.webContents.send('connection-stats', { |
| 1032 | ping: -1, // Indicate error |
| 1033 | download: null, |
| 1034 | upload: null |
| 1035 | }); |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // Function to start ping interval |
| 1041 | function startPingInterval() { |
no outgoing calls
no test coverage detected