How to display ping information of a server on the stage in AS3

977 Views Asked by At

so I have a code to ping a server, but it traces the information. I want to change that, and use dynamic text to display that ping information on the stage. How would I do that? Here is my code:

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);

var url:String = "URL-TO-SITE";
var limit:int = 10;

var time_start:Number;
var time_stop:Number;
var times:int;

ping();

function ping():void
{
    trace("pinging", url);

    times = 0;
    doThePing();
}

function doThePing():void
{
    time_start = getTimer();
    ldr.load(new URLRequest(url));
}

function ldrStatus(evt:*):void
{
    if(evt.status == 200)
    {
        time_stop = getTimer();
        trace("got response in", time_stop - time_start, "ms");
    }

    times++;
    if(times < limit) doThePing();
}
2

There are 2 best solutions below

5
Michell Morsø On

Im not sure if this is what you are asking, but if you want to show the information that is being traced right now, you have to have a textfield on the stage, or create one dynamically. You could do it like this you want to make a text field:

import flash.text.TextField;

function ldrStatus(evt:*):void{

    if(evt.status == 200)
    {
        time_stop = getTimer();
        trace("got response in", time_stop - time_start, "ms");

        var tf:TextField = new TextField();
        addChild(tf);
        tf.text = "got response in"+ (time_stop - time_start) + "ms";
    }

    times++;
    if(times < limit) doThePing();
}
0
sdksmkfnajnf On

It worked, I just made a text box on the stage called textBox, and added some code to the end.

var ldr:URLLoader = new URLLoader();
ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);

var url:String = "http://google.com.net";
var limit:int = 10;

var time_start:Number;
var time_stop:Number;
var times:int;

ping();

function ping():void
{
    trace("pinging", url);

    times = 1;
    doThePing();
}

function doThePing():void
{
    time_start = getTimer();
    ldr.load(new URLRequest(url));
}

function ldrStatus(evt:*):void
{
    if(evt.status == 200)
    {
        time_stop = getTimer();
        textBox.text = String("got response in", time_stop - time_start, "ms");
    }


}