bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/connection-status/component.jsx

58 lines
1.7 KiB
React
Raw Normal View History

import { useEffect, useRef } from 'react';
2024-04-01 20:36:28 +08:00
import { useMutation } from '@apollo/client';
import { UPDATE_CONNECTION_ALIVE_AT } from './mutations';
import { handleAudioStatsEvent } from '/imports/ui/components/connection-status/service';
2023-12-06 00:12:12 +08:00
const ConnectionStatus = () => {
2024-04-01 20:36:28 +08:00
const networkRttInMs = useRef(0); // Ref to store the last rtt
const timeoutRef = useRef(null);
2024-04-01 20:36:28 +08:00
const [updateConnectionAliveAtM] = useMutation(UPDATE_CONNECTION_ALIVE_AT);
const STATS_INTERVAL = window.meetingClientSettings.public.stats.interval;
2024-04-01 20:36:28 +08:00
const handleUpdateConnectionAliveAt = () => {
const startTime = performance.now();
updateConnectionAliveAtM({
variables: {
networkRttInMs: networkRttInMs.current,
},
2024-04-01 20:36:28 +08:00
}).then(() => {
const endTime = performance.now();
networkRttInMs.current = endTime - startTime;
}).finally(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
handleUpdateConnectionAliveAt();
}, STATS_INTERVAL);
});
};
useEffect(() => {
2024-04-01 20:36:28 +08:00
// Delay first connectionAlive to avoid high RTT misestimation
// due to initial subscription and mutation traffic at client render
timeoutRef.current = setTimeout(() => {
handleUpdateConnectionAliveAt();
}, STATS_INTERVAL / 2);
const STATS_ENABLED = window.meetingClientSettings.public.stats.enabled;
if (STATS_ENABLED) {
window.addEventListener('audiostats', handleAudioStatsEvent);
}
return () => {
if (STATS_ENABLED) {
window.removeEventListener('audiostats', handleAudioStatsEvent);
}
};
}, []);
return null;
};
export default ConnectionStatus;