Info
- Periodic check of internet and backend reachability. Code in
ServiceHub/Connectivity
- Backend endpoints: API → Other endpoints
Usage
ConnectivityService: State, StateChanged, IsChecking, IsServiceUnreachable, RequestCheck()
Unknown - not checked yet
NoNetwork - device has no network
NoInternet - network but no internet (checked with Internet Check Urls)
NoServiceConnection - internet works, ServiceHub doesn't answer
Connected
- Checks the backend with
/probe/ping, every 20 s while connected and 4 s otherwise. Any ServiceHub request answered below 500 also counts as connected
- Repeating ServiceHub calls (the lobby server list, heartbeats) wait while
IsServiceUnreachable and go on as soon as the state changes back
- UI:
ConnectivityDisplay - icon per state, click runs a check
Example
private IDisposable _registration;
private ConnectivityService _connectivity;
private void OnEnable()
{
_registration = AppCore.Services.WhenRegistered<ServiceHubManager>(manager =>
{
_connectivity = manager.Connectivity;
if (_connectivity != null)
_connectivity.StateChanged += OnConnectivityChanged;
});
}
private void OnDisable()
{
_registration?.Dispose();
if (_connectivity != null)
_connectivity.StateChanged -= OnConnectivityChanged;
_connectivity = null;
}
Gotchas
ConnectivityService is a module on ServiceHubManager and starts in its LoadRoutine. The manager registers only after that, so Get<ServiceHubManager>() returns null early. Subscribe with AppCore.Services.WhenRegistered<ServiceHubManager> like ConnectivityDisplay, and null check Connectivity
IsServiceUnreachable is false while Unknown. Code that waits on it goes on before the first check finishes
- Empty Service Base Url in
ServiceHubSettings never reaches Connected. A missing app secret doesn't stop the checks
- The ping is a HEAD request and needs 2xx, other API calls count with any answer below 500. A failed call only triggers a new check, it doesn't change the state by itself
NoNetwork comes from Application.internetReachability. NoInternet means none of the Internet Check Urls answered, so a network that blocks them reads as no internet when the hub is down too
- Each check can take up to Check Timeout (4 s) per URL. Without other hub requests a drop shows only at the next 20 s check, plus the check time
- Coming back from pause runs a check right away
StateChanged can still reach a handler that unsubscribed during the same event, guard on your own state