Created
October 10, 2023 15:48
-
-
Save leventeren/9192c8987908f26274552841d16b255e to your computer and use it in GitHub Desktop.
Internet Checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
using Ping = UnityEngine.Ping; | |
public class InternetChecker : SingletonBehaviour<InternetChecker> | |
{ | |
private readonly List<string> _theListOfIPs = new() { "4.2.2.4", "www.google.com" }; | |
private int _counter; | |
[SerializeField] private float Timeout; | |
public async Task<bool> CheckNetwork() | |
{ | |
var internetPossiblyAvailable = Application.internetReachability switch | |
{ | |
NetworkReachability.ReachableViaCarrierDataNetwork => true, | |
NetworkReachability.ReachableViaLocalAreaNetwork => true, | |
NetworkReachability.NotReachable => false, | |
_ => false | |
}; | |
if (!internetPossiblyAvailable) | |
{ | |
return false; | |
} | |
return await CheckPing(); | |
} | |
private async Task<bool> CheckPing() | |
{ | |
_counter = 0; | |
var tcs = new TaskCompletionSource<bool>(); | |
StartCoroutine(Ping(tcs)); | |
await tcs.Task; | |
return tcs.Task.Result; | |
} | |
private IEnumerator Ping(TaskCompletionSource<bool> task) | |
{ | |
var ping = new Ping(_theListOfIPs[_counter]); | |
while (!ping.isDone || Timeout > 0) | |
{ | |
Timeout -= 0.1f; | |
yield return new WaitForSeconds(0.1f); | |
} | |
if (!ping.isDone) | |
{ | |
_counter++; | |
if (_counter >= _theListOfIPs.Count) | |
{ | |
task.SetResult(false); | |
} | |
else | |
StartCoroutine(Ping(task)); | |
} | |
else | |
{ | |
task.SetResult(true); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public class InternetManager : MonoBehaviour | |
{ | |
[SerializeField] private InternetChecker InternetChecker; | |
private async void Start() | |
{ | |
var isOnline = await InternetChecker.CheckNetwork(); | |
Debug.Log($"Is online: {isOnline}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment