Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: A3DStudio on August 17, 2013, 12:10:36 PM

Title: Check for Internet
Post by: A3DStudio on August 17, 2013, 12:10:36 PM
A simple action to check the internet availability on a device.
Title: Re: Check for Internet
Post by: escpodgames on August 17, 2013, 09:32:23 PM
I so need this, thx!
Title: Re: Check for Internet
Post by: jeanfabre on August 19, 2013, 03:34:07 AM
Hi,

 Very cool! is this trick work on all platforms?

bye,

Jean
Title: Re: Check for Internet
Post by: A3DStudio on August 24, 2013, 09:48:52 AM
Yes. It works on all platforms.
Title: Re: Check for Internet
Post by: escpodgames on September 03, 2013, 07:19:39 PM
what if google does down :P?
Title: Re: Check for Internet
Post by: Lane on September 03, 2013, 07:32:43 PM
what if google does down :P?

The earth will stop, and this action would be the least of your worries :P
Title: Re: Check for Internet
Post by: jeanfabre on September 09, 2013, 08:31:22 AM
scary isn't it... cause that would be quite true actually...
Title: Re: Check for Internet
Post by: aLDime on February 18, 2014, 06:16:42 AM
Very useful!
Thanks a lot!
Title: Re: Check for Internet
Post by: davebac on January 01, 2015, 08:58:14 PM
You want to be careful using this trick, as it may not always produce correct results when there is a captive portal, as well as in some other scenarios.

Something like this (Untested, should work) may produce better results:
Code: [Select]
public override void OnEnter()
        {
            try
            {
                //Dns.GetHostEntry("www.google.com");
                var text = UnityEngine.WWW.GetURL("http://www.msftncsi.com/ncsi.txt");
                if (text == "Microsoft NCSI")
                    Fsm.Event(internetIsOn);
                else
                    Fsm.Event(internetIsOff);
            }
            catch
            {
                Fsm.Event(internetIsOff);
            }

            Finish();
        }

Basically...

If you're online somewhere with a click-through agreement, the DNS returns the address of the router until someone opens a web browser, views the page, and clicks the button.  So checking the DNS returns back "yep, I have an Internet connection" -- when, in fact, you don't yet.  And if your program then tries to connect to the master server or whatever, it gets the IP address of the wifi router and not of the actual website you want to connect to.

That's how they get you to look at the click-through page, no matter what you type in the address bar.  You know the page that says "you're entitled to my immortal soul if you continue to access via this hotspot."

Microsoft, Google, Apple and Ubuntu all ran into this -- and all solved it by simply hitting a web page.  Apple ran into another variant of it (having to do with caching on some hotspots), and went to a randomized URL as a result (a traditional cache-buster in the URL)
Title: Re: Check for Internet
Post by: djaydino on April 19, 2015, 12:09:23 PM
this does not seem to work very well on android device :

when i am offline from a fresh restart it gives offline correctly,
then i go online and start the game and it gives online correctly.

but then when i disconnect the internet and start the game again it still gives online as result... i tried editing the url, changed it to my own website, but gives the same result....

greetings,

Dino
Title: Re: Check for Internet
Post by: Weedsh on February 11, 2016, 10:01:42 AM
this code works better for me.

Code: [Select]
try
{
//Dns.GetHostEntry("www.google.com");
using (var client = new WebClient())
{
using (var stream = client.OpenRead("http://www.google.com"))
{
Fsm.Event(internetIsOn);
}
}
Finish();
}
catch
{
Fsm.Event(internetIsOff);
Finish();
}
Title: Re: Check for Internet
Post by: dudebxl on February 12, 2016, 06:03:19 AM
I use WWWPostMobile + url http://www.msftncsi.com/ncsi.txt

Then i check if it returns 'Microsoft NCSI' value. Seems to work well and no need to make an action. Like davebac says..

What is NCSI?
NCSI stands for Network Connectivity Status Indicator. It is part of what Microsoft calls Network Awareness. Microsoft purposed Network Awareness to provide network-connectivity information to services and applications running on Windows Vista and Windows 7.
Title: Re: Check for Internet
Post by: Weedsh on February 19, 2016, 05:08:35 PM
But my code (action) works on android ;)

I donĀ“t know if your method works on android.
Title: Re: Check for Internet
Post by: dudebxl on February 20, 2016, 11:46:50 AM
Hi,

Yes it does.. and I thank you for your code.. just giving alternative way of doing it.  :D
Title: Re: Check for Internet
Post by: Christoph on October 19, 2020, 05:52:42 PM
Is there a good way to do this nowadays?

I can't make it work with the new UnityEngine.Networking could anyone update that code please?
Title: Re: Check for Internet
Post by: djaydino on October 20, 2020, 05:42:38 AM
Hi.

There is an action on the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181) :

(https://i.imgur.com/So1C0Qt.png)

I have not use this yet, but it probably works :)
Title: Re: Check for Internet
Post by: Christoph on October 20, 2020, 05:32:35 PM
Yeah thanks Djaydino, but I looked at that one but couldn't install it (never appeared in the list). So not sure what's wrong with it.

I also was looking for something much simpler like the one posted here, just that Unity changed all the network stuff and I couldn't find out how to update it with my limited knowledge.
Title: Re: Check for Internet
Post by: Broken Stylus on December 10, 2020, 04:55:43 PM
Bumped for the greater good. Is this action reliable for iOS/Android?
I wonder what would be the simplest and surest method to check on the availability of a connection.

Edit.
https://hutonggames.com/playmakerforum/index.php?topic=22980.0
Has there been an update on this lately?
Title: Re: Check for Internet
Post by: coxy17 on October 09, 2022, 05:19:45 AM
this asset has playmaker actions https://assetstore.unity.com/packages/tools/utilities/online-check-pro-74688
Title: Re: Check for Internet
Post by: Christoph on October 17, 2022, 11:56:00 PM
This is what I use nowadays. Seems to work the best:

Code: [Select]
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;

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

public class CheckForInternetConnection : FsmStateAction
{
[Tooltip("Send event if internet is available.")]
public FsmEvent internetIsOn;

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

[Tooltip("Sets a bool to true or false depending on the state of the Internet")]
public FsmBool isInternetOn;


public override void Reset()
{
internetIsOn = null;
internetIsOff = null;
isInternetOn = true;
}

public override void OnEnter()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
isInternetOn.Value = false;
Fsm.Event(internetIsOff);
}
else
{
isInternetOn.Value = true;
Fsm.Event(internetIsOn);
}
}
}
}
Title: Re: Check for Internet
Post by: amdev on December 25, 2022, 09:00:52 AM
Code: [Select]

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Custom")]
    [Tooltip("Checks if there is an internet connection on the Android device.")]
    public class CheckInternetConnection : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        [Tooltip("Store the result of the check in a boolean variable.")]
        public FsmBool internetAvailable;

        public override void OnEnter()
        {
            // Check for internet connection
            CheckConnection();
        }

        void CheckConnection()
        {
            // Set up a UnityWebRequest to check for internet connection
            UnityWebRequest www = UnityWebRequest.Get("http://google.com");
            www.SendWebRequest();

            // Check for internet connection
            if (www.isNetworkError || www.isHttpError)
            {
                // There is no internet connection
                internetAvailable.Value = false;
            }
            else
            {
                // There is an internet connection
                internetAvailable.Value = true;
            }
        }
    }
}