import React from "react"; import { SocketProvider } from "../components/SocketProvider"; import { useConnection } from "../hooks/useConnection"; /** * Example component demonstrating simple ping functionality with periodic pinging */ function PingMeasurementDemo() { const { status, connect, disconnect, isConnected, ping } = useConnection(); const [lastRTT, setLastRTT] = React.useState(null); const [isLoading, setIsLoading] = React.useState(false); const [isPeriodicPingEnabled, setIsPeriodicPingEnabled] = React.useState(false); const [pingHistory, setPingHistory] = React.useState([]); const intervalRef = React.useRef(null); ``; const handleConnect = () => { connect().catch(console.error); }; const handleDisconnect = () => { disconnect(); }; const handlePing = async () => { if (!isConnected()) return; setIsLoading(true); try { const rtt = await ping(); setLastRTT(rtt); // Keep history of last 10 pings setPingHistory((prev) => [...prev.slice(-9), rtt]); } catch (error) { console.error("Ping failed:", error); setLastRTT(null); } finally { setIsLoading(false); } }; const startPeriodicPing = () => { if (intervalRef.current) return; // Already running setIsPeriodicPingEnabled(true); intervalRef.current = setInterval(async () => { if (isConnected()) { try { const rtt = await ping(); setLastRTT(rtt); setPingHistory((prev) => [...prev.slice(-9), rtt]); } catch (error) { console.error("Periodic ping failed:", error); } } }, 5000); // Ping every 5 seconds }; const stopPeriodicPing = () => { if (intervalRef.current) { clearInterval(intervalRef.current); intervalRef.current = null; } setIsPeriodicPingEnabled(false); }; // Cleanup interval on unmount or disconnect React.useEffect(() => { if (!isConnected()) { stopPeriodicPing(); } return () => { if (intervalRef.current) { clearInterval(intervalRef.current); } }; }, [isConnected()]); return (

Ping Measurement Demo

Connection Status

Status: {status}

Connected: {isConnected() ? "Yes" : "No"}

Ping Measurement

Last RTT:{" "} {lastRTT !== null ? `${lastRTT.toFixed(2)}ms` : 'Click "Ping Server" to measure latency'}

{/* Average RTT */} {pingHistory.length > 0 && (

Average RTT:{" "} {( pingHistory.reduce((sum, rtt) => sum + rtt, 0) / pingHistory.length ).toFixed(2)} ms ({pingHistory.length} samples)

)} {/* Connection Quality Indicator */} {lastRTT !== null && (
Connection Quality:{" "} {lastRTT < 50 ? "Excellent" : lastRTT < 150 ? "Good" : "Poor"}
)} {/* Periodic ping status */} {isPeriodicPingEnabled && (

🔄 Auto-pinging every 5 seconds...

)} {!isConnected() && (

Connect to the server to measure ping latency

)}
{/* Ping History Chart */} {pingHistory.length > 1 && (

RTT History

{pingHistory.map((rtt, index) => { const maxRTT = Math.max(...pingHistory); const height = Math.max((rtt / maxRTT) * 80, 5); // Min 5px height return (
); })}

Last {pingHistory.length} pings • Hover bars for exact values

)}

How It Works

  • Manual Ping: Click "Ping Server" for instant measurement
  • Auto Ping: Enable periodic pings every 5 seconds for continuous monitoring
  • Smart Tracking: Keeps history of last 10 pings with average calculation
  • Visual Feedback: Color-coded quality indicators and RTT history chart
  • Promise-based: Clean async/await pattern with error handling
  • Efficient: Uses sequential ping IDs for reliable response matching
); } /** * Full example with SocketProvider wrapper */ export function PingMeasurementExample() { return ( ); } export default PingMeasurementExample;