Skip to content

Check against posthog server (or other, ie. google.com) using simple ping before calling NetworkClient's SendBatch #133

Description

@GDorneAtLaerdal

Problem Statement

In EventQueue.cs's FlushCoroutine, the Application.internetReachability == NetworkReachability.NotReachable check does not actually always determine whether internet connection (especially to a specific server) is available and working.
On top of this first cheap check, I suggest to have another cheap check following it which just pings against any relevant servers, before running NetworkClient's SendBatch logic. If check fails, apply same logic as the internetReachability check.

Solution Brainstorm

You could use Task or Coroutine (see link below for simple example using coroutine). UniTask is great for this kind of stuff too but I don't think you guys are using it.
https://stackoverflow.com/a/57553685

See the code below for UniTask implementation based on the link above

public static async UniTask<bool> CheckInternetConnection(CancellationToken cancellationToken = default)
        {
            const string echoServer = "http://google.com";

            using (var request = UnityWebRequest.Head(echoServer))
            {
                request.timeout = 5;

                try
                {
                    await request.SendWebRequest().WithCancellation(cancellationToken);
                }
                catch (UnityWebRequestException)
                {
                    return false;
                }

                return (request.result != UnityWebRequest.Result.ConnectionError) && (request.result != UnityWebRequest.Result.ProtocolError) && request.responseCode == 200;
            }
        }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions