PDA

View Full Version : How can I ping a host using as3



sci
March 17th, 2010, 10:11 AM
Any ideas? Thanks

alaricoffir
March 17th, 2010, 12:09 PM
Sure, just call the host and have it return a Boolean.

Do you need help calling a host? Take a look at this:

http://www.flash-db.com/Tutorials/helloAS3/

komapeb
March 17th, 2010, 12:11 PM
You can try with URLLoader and getTimer() Here's a quick example:


package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.utils.*;

public class Main extends Sprite
{
private var loader:URLLoader;
private var req:URLRequest;
private var t:uint;

private var count:uint;
private var currentCount:uint;

public function Main():void
{
count = 10;
req = new URLRequest('http://google.com');
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, pingComplete);
loader.load(req);
t = getTimer();
}

private function pingComplete(e:Event):void
{
currentCount++;
trace(getTimer() - t, 'ms');
if (currentCount < count) {
loader.load(req);
t = getTimer();
}
}
}

}

To use it just paste it in a file called "Main.as" and set it as your Document class.

sci
March 17th, 2010, 12:23 PM
thanks guys... massive help.

justkevin
March 17th, 2010, 12:28 PM
It depends on what you mean by "ping". Do you mean: A real ICMP ping, a test whether a web resource is available, a web latency test or a tcp/ip socket latency test?

sci
March 17th, 2010, 01:31 PM
erm... not sure, apologies for my ignorance on this matter. I am prototyping a project that uses the responsiveness of my internet connection to drive an animation. In its simplest form I was thinking I could just ping a host and run the animations off that returned value.

justkevin
March 17th, 2010, 08:16 PM
Is the project to show the general responsiveness of your internet connection, or responsiveness of a specific host? If the former, you'll probably want to average the response from a many unrelated hosts.

sci
March 18th, 2010, 06:27 AM
the former... I would just like to measure the performance of my connection and illustrate this, using flash, with an animated 'diagram'.

aidanmack
December 6th, 2011, 11:05 AM
Hey,
Just looking for method to ping a server myself..

I originally thought of this method too....

loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onSuccess);
loader.addEventListener(IOErrorEvent.IO_ERROR, onFail);
loader.load(new URLRequest("http://www.google.com"))


**WARNING**
To anyone that reads this through search, flash will download the entire contents of the site you ping to the temp folder.


.. I found out the hardway. I was pinging... If thats what you can call it, engadget.com every 10 - 15 seconds. This downloaded the contents of engadgets home page to the temp folder ever 10-15 secs... oopps... Destroyed the users bandwidth.

maxwarch
January 19th, 2012, 04:52 AM
Hello, with a static function, you can call this :



public static function ping(url:String, callback:Function):void
{
var loader:URLLoader = new URLLoader();

loader.addEventListener(Event.COMPLETE,
function(e:Event)
{
e.currentTarget.removeEventListener(Event.COMPLETE , arguments.callee);
e.currentTarget.removeEventListener(IOErrorEvent.I O_ERROR, arguments.callee);
URLLoader(e.currentTarget).close();
callback(true);
},
false, 0, true);

loader.addEventListener(IOErrorEvent.IO_ERROR,
function(e:IOErrorEvent)
{
e.currentTarget.removeEventListener(Event.COMPLETE , arguments.callee);
e.currentTarget.removeEventListener(IOErrorEvent.I O_ERROR, arguments.callee);
URLLoader(e.currentTarget).close();
callback(false);
},
false, 0, true);

try
{
loader.load(new URLRequest(url));
}
catch (error:Error)
{
callback(false);
}
}




usage :



yourstaticclass.ping("http://www.google.com", test);


function test(rep:Boolean):void
{
trace(rep);
}