Skip to content

C.Network

Aprius edited this page Aug 27, 2024 · 1 revision

Method check connection is very simple to use.

Methods like Unity's Application.internetReachability cannot truly tell if a device is actually connected to the internet, and neither was designed to do so. Moreover, simple methods like pinging google are unreliable since devices could appear to be connected to the internet, but in reality be behind a restricted network.

In this case a method called Captive Portal Detection solves this problem. It can quickly and reliably determine the current internet connection. Caprive Portal Detection is a technique that is used in all major operating systems for detecting internet connectivity.

CheckConnection return states include:

  • PendingCheck : A network check has not being performed yet, or it is currently in progress for the first time
  • NoDnsConnection : No connection could be established to a valid DNS destination
  • WalledGarden : General network connection was established, but target destination could not be reached due to restricted internet access.
  • Connected : Network connection was established succesfully
using Pancake.Common;

C.Network.CheckConnection(OnCompleted);

private void OnCompleted(ENetworkStatus status)
{
  switch (status)
  {
    case ENetworkStatus.PendingCheck:
     // TO_DO
     break;
    case ENetworkStatus.NoDnsConnection:
     // TO_DO
     break;
    case ENetworkStatus.WalledGarden:
     // TO_DO
     break;
    case ENetworkStatus.Connected:
     // TO_DO
     break;
    default:
     // TO_DO
     throw new ArgumentOutOfRangeException(nameof(status), status, null);
  }
}

Or

using Pancake.Common;

C.Network.CheckConnection(_ =>
{ 
  switch (_)
  {
    case ENetworkStatus.PendingCheck:
     // TO_DO
     break;
    case ENetworkStatus.NoDnsConnection:
     // TO_DO
     break;
    case ENetworkStatus.WalledGarden:
     // TO_DO
     break;
    case ENetworkStatus.Connected:
     // TO_DO
     break;
    default:
     // TO_DO
     throw new ArgumentOutOfRangeException(nameof(_), _, null);
  }
});
Clone this wiki locally