Playmaker Forum
PlayMaker Updates & Downloads => Share New Actions => Topic started by: A3DStudio on August 17, 2013, 12:10:36 PM
-
A simple action to check the internet availability on a device.
-
I so need this, thx!
-
Hi,
Very cool! is this trick work on all platforms?
bye,
Jean
-
Yes. It works on all platforms.
-
what if google does down :P?
-
what if google does down :P?
The earth will stop, and this action would be the least of your worries :P
-
scary isn't it... cause that would be quite true actually...
-
Very useful!
Thanks a lot!
-
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:
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)
-
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
-
this code works better for me.
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();
}
-
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.
-
But my code (action) works on android ;)
I donĀ“t know if your method works on android.
-
Hi,
Yes it does.. and I thank you for your code.. just giving alternative way of doing it. :D
-
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?
-
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 :)
-
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.
-
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?
-
this asset has playmaker actions https://assetstore.unity.com/packages/tools/utilities/online-check-pro-74688
-
This is what I use nowadays. Seems to work the best:
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);
}
}
}
}
-
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;
}
}
}
}