-
Notifications
You must be signed in to change notification settings - Fork 17
NetworkPinger
Jimmy Cushnie edited this page Jan 26, 2021
·
1 revision
NetworkPinger is a utility for testing connectivity to an IP address. It sends a series of ICMP pings to a given destination. If any of them fail, it will invoke a failure callback with the reason for the failure. If they all succeed, it will invoke a success callback with the average round-trip time of the pings.
NetworkPinger is primarily intended for in-game server lists, where you want to see whether you can connect to a server and if so what your ping is.
Note: IPv6 addresses are currently unsupported due to a Unity bug. If and when that's fixed I should be able to add IPv6 support.
public void LogPing(IPAddress pingThisAddress)
{
var pinger = new NetworkPinger(pingThisAddress);
pinger.SendPing
(
onPingSuccessCallback: success =>
{
Debug.Log($"Ping to {pingThisAddress} succeeded; average round-trip time was {success.AverageRoundTripTimeMilliseconds}ms");
},
onPingFailureCallback: failure =>
{
Debug.Log($"Ping to {pingThisAddress} failed; {failure.FailureReason}");
}
);
}
You can also pass the function custom values for the number of separate ping packets to send and the timeout for ping failure.