playMaker

Author Topic: Check for Internet  (Read 4561 times)

A3DStudio

  • Playmaker Newbie
  • *
  • Posts: 43
    • View Profile
    • A3DStudio
Check for Internet
« on: August 17, 2013, 09:10:36 AM »
A simple action to check the internet availability on a device.
« Last Edit: August 17, 2013, 11:14:23 AM by A3DStudio »

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
Re: Check for Internet
« Reply #1 on: August 17, 2013, 06:32:23 PM »
I so need this, thx!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: Check for Internet
« Reply #2 on: August 19, 2013, 12:34:07 AM »
Hi,

 Very cool! is this trick work on all platforms?

bye,

Jean

A3DStudio

  • Playmaker Newbie
  • *
  • Posts: 43
    • View Profile
    • A3DStudio
Re: Check for Internet
« Reply #3 on: August 24, 2013, 06:48:52 AM »
Yes. It works on all platforms.

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
Re: Check for Internet
« Reply #4 on: September 03, 2013, 04:19:39 PM »
what if google does down :P?

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2484
  • Yup.
    • View Profile
    • Cleverous
Re: Check for Internet
« Reply #5 on: September 03, 2013, 04:32:43 PM »
what if google does down :P?

The earth will stop, and this action would be the least of your worries :P

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12344
  • Official Playmaker Support
    • View Profile
Re: Check for Internet
« Reply #6 on: September 09, 2013, 05:31:22 AM »
scary isn't it... cause that would be quite true actually...

aLDime

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
    • http://aldimegame.com/
Re: Check for Internet
« Reply #7 on: February 18, 2014, 04:16:42 AM »
Very useful!
Thanks a lot!

davebac

  • Playmaker Newbie
  • *
  • Posts: 3
    • View Profile
Re: Check for Internet
« Reply #8 on: January 01, 2015, 06: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)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 2265
    • View Profile
    • jinxtergames
Re: Check for Internet
« Reply #9 on: April 19, 2015, 09:09:23 AM »
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

Weedsh

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Re: Check for Internet
« Reply #10 on: February 11, 2016, 08: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();
}

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Check for Internet
« Reply #11 on: February 12, 2016, 04: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.
« Last Edit: February 20, 2016, 09:46:04 AM by dudebxl »

Weedsh

  • Playmaker Newbie
  • *
  • Posts: 24
    • View Profile
Re: Check for Internet
« Reply #12 on: February 19, 2016, 03:08:35 PM »
But my code (action) works on android ;)

I donĀ“t know if your method works on android.

dudebxl

  • Hero Member
  • *****
  • Posts: 602
    • View Profile
Re: Check for Internet
« Reply #13 on: February 20, 2016, 09:46:50 AM »
Hi,

Yes it does.. and I thank you for your code.. just giving alternative way of doing it.  :D