- Home /
how to Ping Google.com
i want to check internet speed to google, then how to get return value of ping time? i can't figure out the syntax so i tried :
function Update(){
Debug.Log(Ping("www.google.com").time);}
but debug always return -1, please give me help of sample script..
Answer by syclamoth · Jan 31, 2012 at 06:01 AM
The correct usage of 'Ping' is asynchronous- what you're trying to do here is start a fresh Ping every frame, without waiting for it to return anything!
Instead, use it like this:
function Start() { PingGoogle(); }
function PingGoogle() { var googPing : Ping = new Ping("74.125.224.72"); // keep in mind that 'Ping' only accepts IP addresses, it doesn't // do DNS lookup. This address may not work for your location- // Google owns many servers the world over.
while(!googPing.isDone)
{
yield;
}
Debug.Log(googPing.time);
}
O$$anonymous$$ thanks a lot, btw you missed "new" before Ping("74.125.224.72")
Sorry, I always forget where it's necessary in JS. It's easy in C#- just use them everywhere...
in c# for people: Ping GooglePing = new Ping("74.125.224.72"); while(!GooglePing.isDone) { yield return null; }
Hello! This JS was really helpful - but does anyone know how to take the result 'googPing.time' into a FLOAT or an INT?
I tried many times, but I always get '0' as the result.
I'm going to ping my own server, then I'm needing to put a time delay, calculated - it will take the ping value - for ex. 80, then divide it by 8, resulting with the value 10 - then put a delay for 10 seconds - which is about how much time my assets need to load (in extensive tests done), at that (ping/based) speed.
I've tried: ... Debug.Log(googPing.time);
goog = parseInt(googPing.time);
yield goog;
}
but then goog can not be used as a divider - such as:
var divider = 8;
timer = ( goog / divider); //Divides goog by 8
//unfortunately the result is always 0
If you're trying to ping your Amazon AWS EC2 instance and get a reply of -1, make sure you add this Inbound rule to your Security Group: Custom IC$$anonymous$$P Rule: ECHO REQUEST, Source: Anywhere. Or you will simply not get a reply and thus get a ping.time of -1
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Is it possible to check internet conneciton for WWW class downloading ? 3 Answers
Unity multiplayer over internet does not work using port forwarding 1 Answer
Connecting to custom Master Server over the internet 1 Answer
How does/can Unity handle offline sync with backend? 2 Answers