playMaker

Author Topic: Check for Internet Connection (action) with UnityEngine.Networking  (Read 1140 times)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
So I am trying to set up an action to check if Internet is available or not, using a coroutine with the "new" UnityEngine.Networking. I got it almost working 100% but for some reason I have one situation that messes with the execution of the code.

This is the code I'm using:
Code: [Select]
using UnityEngine.Networking;
using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Device)]
[Tooltip("Check for internet connection.")]

public class CheckForInternetConnection : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]

        [Tooltip("Send event if internet is available.")]
public FsmEvent internetIsOn;

        [Tooltip("Send event if internet is not available.")]
public FsmEvent internetIsOff;

public override void OnEnter()
{
StartCoroutine(CheckInternetConnection());
}


private IEnumerator CheckInternetConnection()
{
UnityWebRequest www = UnityWebRequest.Get("http://www.msftncsi.com/ncsi.txt");
yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError || www.downloadHandler.text != "Microsoft NCSI")
{
Fsm.Event(internetIsOff);
}
else if (www.downloadHandler.text == "Microsoft NCSI")
{
Fsm.Event(internetIsOn);
}
}
}
}
I have set up that action on a button click so I quickly can test if it works or not.

When I open the game while Internet is OFF and I click on the button, it works and returns Internet OFF. I even can turn the Internet ON and click again on the button and it now returns Internet ON as it should.

When I open the game while Internet is ON and I click on the button, it works and returns Internet ON. But... (the problem)

When I disable Internet now and then click again to test if it is OFF, it then just stays forever on the state with the action attached to it and never triggers any of the two events. Like if the coroutine never gets executed completely and stays at the "SendWebRequest" stage.

I then can click again a second time on the button and it works again as expected showing Internet as OFF. It just is that once it has checked already and Internet was ON, it doesn't recognize Internet being OFF the first time, respectively something prevents it from executing the code entirely.

Any idea why this might be happening?
« Last Edit: November 02, 2020, 02:48:23 PM by Christoph »

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Check for Internet Connection (action) with UnityEngine.Networking
« Reply #1 on: May 13, 2022, 08:48:44 PM »
Long time still the same problem... anyone has an idea what's wrong with the above code?