Testing for Active Internet Connection

So i’ve been looking for a intelligent way of attacking this concept. Currently my search has revealed the following pages:

First hit

Linked

Searching through the Refrence materials Yielded this: TestConnection

After placing and trying out the code @ TestConnection, I still had errors spitting if I didn’t have an active connection. It seems that unity lacks the basic function of checking for a Active Internet Connection.

Even the Obvious goto of Application.internetReachability apears to be useless by their own admission:
" Do not use this property to determine the actual connectivity. " Which really begs another question of “Why does is exist then?” I don’t really actually care about finding that out though.

This search is getting tiresome. I simply want to quickly check for a active internet connection. That way my code won’t generate errors when I seek to establish a server without a connection open, it will simply inform me that the operation isn’t possible.

I am familiar with Javascript/Unityscript if you could limit your answers to that I’d be most appreciative.

I did a bit of research after pure .NET (C#) solution, and it seems the common answer is to ping, or better try connecting to specific server, like the server you want to contact, or google server. Here’s code from one of the answers on StackOverflow, converted to JavaScript:

function CheckForInternetConnection()
{
	var client : System.Net.WebClient;
	var stream : System.IO.Stream;
    try
    {
        client = new System.Net.WebClient();
        stream = client.OpenRead("http://www.google.com");
        return true;
    }
	catch (ex)
	{
		return false;
	}
	finally
	{
		if(client) { client.Dispose(); }
		if(stream) { stream.Dispose(); }
	}
}

I tested in under Windows and it works ok. Instead of testing against google server, you can of course use different well established address, or an address of the server you want to contact (unless this is code for server of course).

You can also change the method to accept url parameter, and test it for two or three addresses, in case you’re afraid google.com could get down. But I guess if that happens, we’re doomed anyway :wink:

Put your connection in a try block and if an error is caught(no connection) you just don’t continue.

this link shows code that checks if you have no network, are being redirected (to a secondary login page for example) or have full internet connection

http://answers.unity3d.com/questions/567497/how-to-100-check-internet-availability.html#answer-567499

To truly know you’re online, you need to implement “captive portal detection”, to know if you’re e.g. hitting a public WiFi login page. So just checking Application.internetReachability or doing a Ping to some address doesn’t guarantee you can successfully make connections or make WWW requests.

I have made an easy asset called Internet Reachability Verifier. It keeps you up-to-date whether you have verified internet access (WWW requests can be done).
More info here: http://j.mp/IRVUN