- Home /
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:
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.
As silly as it sounds, can't you just use http://docs.unity3d.com/Documentation/ScriptReference/WWW.html to check if you can connect to some site or is that too slow / not exactly what you were looking for?
What Application.internetReachability checks, if there is no connection, a data packet connection, or a wifi/ethernet connection possibility. It doesn't check if the device is connected or not.
I agree, it could be named better.
@ShadoX Its close, and I might be able to get it to work but, even you have to admit that its a dirty workaround. I'll see what I can make of it.
@Jamora Yeah I agree, but since any application you are planning to make utilizing a connection requires a ACTUAL connection, its completely useless. I wish they had given us what we need ins$$anonymous$$d. Thanks for the information though.
Something worth trying is to check if Debug.Log(Network.player.ipAddress);
returns a valid IP address. It does for this computer, but I can't currently test it on a not-connected computer, so I dunno what it returns then...
@Jamora tried it and recieved the same IP both connected and unconnected. It was worth a try though ^.^
Answer by ArkaneX · Sep 13, 2013 at 10:59 PM
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 ;)
I'll have to doublecheck this tomorrow when I get home but, it should function for what I'll be needing. Workaround or no, I really want to getting this fixed so I can get rolling on the front-end for the project I'm putting together. Thanks for bearing with me.
Side note: if the apocalypse strikes I will miss three things most of all... 1. Google 2. DIY YouTube 3. Toilet Paper. Whats your list?
Heh - never thought about this, but one thing for sure: internet.
Alright i'm back from my trip (had to stay for a few extra weeks) and setup this code, and it always returns true even when the internet connection is disabled... I went a step further to disable my router, and it provides the only viable internet source since I live in the woods. So... Thoughts?
Could you check what are headers of your response? Put this just after calling client.OpenRead:
if (client.ResponseHeaders != null)
{
for (var i : int = 0; i < client.ResponseHeaders.Count; i++)
{
print(client.ResponseHeaders.$$anonymous$$eys[i] + ": " + client.ResponseHeaders[i]);
}
}
else
{
print("null headers");
}
Heh... I don't know why it doesn't throw exception (in my tests it does), but it seems that ins$$anonymous$$d of
return true;
you can use
return client.ResponseHeaders != null && client.ResponseHeaders.Count > 0;
This should work correctly, if headers are returned only when the machine is connected.
Anyway - I suggest to be careful and test it on different machines as well. Also, please note that OpenRead is synchronous operation, so theoretically it could block your game. To prevent this, you can use OpenReadAsync with OpenReadCompleted event ins$$anonymous$$d.
Answer by Slobdell · Sep 11, 2013 at 07:34 PM
Put your connection in a try block and if an error is caught(no connection) you just don't continue.
Can you give a JavaScript example... I've never heard of a try block before.
Oh. Sure it's easy
try{
//put your connection code here
// if it can't connect it will throw an error
}catch(error){
//this code gets executed only if an error is thrown
}
Basically it allows errors, in your case connection errors, and you can deal with them ins$$anonymous$$d of just having your application crash.
There's tons of info online about try catch blocks but thats basically all it is
That is a nice little piece of info. I've been $$anonymous$$ching myself to code, and I miss a lot of gems like this. I will try it out and return with a ruling. I'll definitely toss you a few thumbs up regardless for filling in a info gap!
Alright I tried using it, but catch(error) is not ever called. The error shows up in the console but fails to trigger the code. Here is my code:
try{
connectionTestResult = Network.TestConnection();
}
catch(UnityException) {
Debug.Log("Internet is not available.");
}
$$anonymous$$y error code with the internet connection disabled is:
Cannot resolve connection tester address, you must be connected to the internet before perfor$$anonymous$$g this or set the address to something accessible to you. UnityEngine.Network:TestConnection() Servertesting:TestConnection() (at Assets/2d/Scripts/Servertesting.js:35) Servertesting:OnGUI() (at Assets/2d/Scripts/Servertesting.js:27)
So basically I'm wondering why the catch isn't working. I've done some poking around and replaced catch(error) with:
catch(err)
catch(UnityException)
catch(e)
Does the catch function even work with Unity?
I think I'll also post this as a separate question.
Answer by naglers · Nov 01, 2013 at 04:55 PM
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
Answer by tonic · Jun 27, 2014 at 02:34 PM
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
Your answer
Follow this Question
Related Questions
How to set up multiplayer? 2 Answers
Only Editor Can Connect with Standalone's Server 1 Answer
connect with Server(Host) 0 Answers
How can i get real "extern" IP address ? 2 Answers
Can't connect to localhost server - connection request failed 3 Answers