# Ping Client

## Overview

The Ping Client is a utility class in the coherence SDK that allows you\
to measure network latency to coherence servers. It supports both UDP\
and TCP protocols and provides configurable timeout and cancellation\
token support for robust latency testing.

{% hint style="warning" %}
Due to using raw TCP/UDP sockets, currently PingClient does not support the WebGL platform.
{% endhint %}

## Usage

When you fetch regions using `RegionsService.FetchRegionsInfoAsync`, you\
receive both the available regions and their associated ping servers. This data can then be used in the PingClient in order to measure round-trip time to the respective regions:

```csharp
PlayerAccount playerAccount = await PlayerAccount.GetMainAsync();

// Fetch information about regions available in our Project.
var regionsService = playerAccount.Services.Regions;
var regions = await regionsService.FetchRegionsAsync();

// Ping all available regions.
var pingResults = await PingClient.PingAsync(regions);

foreach (var ping in pingResults)
{
    if (ping.Success)
    {
        Debug.Log($"Region {ping.Region} has round-trip time of {ping.RoundTripMs}ms");
    }
    else
    {
        Debug.LogWarning($"Failed to ping region {ping.Region}: {ping.Error}");
    }
}
```

## Notes

* `PingClient.PingAsync` optionally accepts:
  * `PingProtocol` enum to specify which protocol to use for pinging
  * Timeout value in milliseconds to specify the maximum time spent pinging
  * Cancellation token used for aborting the ping operation
* The result is an average over multiple pings made internally within a single call to `PingClient.PingAsync`
* Multiple regions are pinged in parallel for efficiency
