Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

Question Testing if internet access is available

Discussion in 'Scripting' started by pKallv, Jun 23, 2022.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,046
    I am currently testing this code for this:

    Code (CSharp):
    1. IEnumerator CheckInternetConnection()
    2.     {
    3.         UnityWebRequest request = new UnityWebRequest("http://google.com");
    4.         yield return request.SendWebRequest();
    5.         if (request.error != null)
    6.         {
    7.             Debug.Log("*** NO INTERNET ***");
    8.             isInternetActive = false;
    9.         }
    10.         else
    11.         {
    12.             Debug.Log("*** INTERNET AVAILABLE ***");
    13.             isInternetActive = true;
    14.         }
    15.     }
    However, it takes quite a long time (10+ seconds), in the editor, to finish. I guess if players must wait this amount of time to play I have a problem.

    How do you handle this question and what is the best and fastest way of checking this?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,727
    I don't do this (I never needed if internet is enabled or not for the games I worked on), but in your case they invented timeout for this. You trade accuracy (slow internet connection can be detected as offline) for speed. As usual.
     
    pKallv likes this.
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,046
    Thanks.
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    897
    The downside about timeout is, that when you download large files and the timeout has been reached it will cancel the ongoing download. Even though you have a full speed connection going. Once the timeout is reached it will automatically throw. It does not reset in any way, once the download starts, it starts counting. No matter whether you have a good or bad connection.
    You'd have to monitor the amount of bytes that are downloaded.

    UnityWebRequest
    does not separate errors, nor does it throw any usable exceptions.
    All you know is that the request failed. Not what happened with it unless you specifically read out values like the response code and the error string.

    I've already suggested features for UnityWebRequest on the Roadmap. It would sure make handling failing / slow requests a lot easier. As well for UI.
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,727
    For this use case, none of these matter. Pinging if there is internet connection involves downloading the smallest content possible. So theoretically resume or fails don't matter. Or even result content.
     
    pKallv likes this.
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,046
    Correct I just want to ping internet in the shortest time possible to setup my game correctly for single- vs. multiplayer.
     
  7. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,046
    Hmmm every time I test without internet editor hangs.
     
unityunity