Search Unity

Check internet Subway

Discussion in 'Scripting' started by Hercull, Nov 20, 2017.

  1. Hercull

    Hercull

    Joined:
    Aug 28, 2017
    Posts:
    42
    Hi Guys,

    Player of my game say me they got a problem when they play on the subway and the connection didn't work properly.
    I send request WWW form to my script php and the response is wrong.

    Like this:

    WWWForm form = new WWWForm();
    form.AddField("field1", field1);
    WWW w = new WWW(URL, form);
    yield return w;
    if (w.error != null)
    {
    Txt1.text = "Connection problem. Please check your internet connection.";
    }
    else
    {
    JsonData json = JsonMapper.ToObject(w.text);

    if (json["return"].ToString() == "ok")
    {
    //do the job
    }
    else
    {
    print("Problem.");
    print(w.text);
    }
    w.Dispose();
    }

    I tried when disable internet manually its work perfectly, but when they are on the subway they got no answer.

    What you think of checking www.responseHeaders to know if internet is ok or not?

    How you test your internet connectivity on the subway ?

    TY
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    When you're checking for an error don't use:

    Code (CSharp):
    1. if( w.error != null )
    as it doesn't cover all the cases, especially on mobile platforms. You need to use

    Code (CSharp):
    1. if (!string.IsNullOrEmpty(w.error))
    2. {
    3. // An error has occurred
    4. }
    5. else
    6. {
    7. // No error has occurred
    8. }
    This is because w.error may not be null but it might be empty (which is ok and not an error).

    The only true way to check internet connectivity is the way you're doing it (by trying to access a site). Having wifi or mobile data availability isn't a good test as you can't actually tell until you try to access a site.
     
  3. Hercull

    Hercull

    Joined:
    Aug 28, 2017
    Posts:
    42
    TY for this answer its was what I was looking for.
     
  4. Hercull

    Hercull

    Joined:
    Aug 28, 2017
    Posts:
    42
    Hi, i tested myself on subway with:

    if (!string.IsNullOrEmpty(w.error))

    I got the same result if you have another idea to fix this will be great.

    TY