playMaker

Author Topic: Check for Internet  (Read 20007 times)

A3DStudio

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

escpodgames

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

jeanfabre

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

 Very cool! is this trick work on all platforms?

bye,

Jean

A3DStudio

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

escpodgames

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

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Check for Internet
« Reply #5 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
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

jeanfabre

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

aLDime

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

davebac

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Check for Internet
« Reply #8 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)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Check for Internet
« Reply #9 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

Weedsh

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Check for Internet
« Reply #10 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();
}

dudebxl

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

Weedsh

  • Playmaker Newbie
  • *
  • Posts: 24
Re: Check for Internet
« Reply #12 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.

dudebxl

  • Hero Member
  • *****
  • Posts: 602
Re: Check for Internet
« Reply #13 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

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Check for Internet
« Reply #14 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?