Search code examples
flashactionscript-3actionscriptflash-cs3

I need to ping to an network with flash or actionscript


I have created a network trouble shooting tool in flash. The design will have all the componenets on the screen. I have to ping to every component once in minute. I have finished the design part.

Please someone help me how do i ping a webaddress or IP in flash.

I need a sample code.. Im using Flash CS3


Solution

  • What do you mean by you have all the components on the screen and you have to ping every component once in a minute?

    If by ping you mean an app, what checks the time-response of a url, then you can try to play with this 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();
    }
    

    This is nothing special, the URLLoader tries to load the url, and listens to the response. If the status is 200, then got a successful "ping". Or pong.

    On the other hand, you can always run a server-side ping program, and control that with flash.

    If you mean an app, like an upload-download-speedtester, that also starts with something like this, but rather with the Loader object.

    Hope this helps.

    EDIT:

    Preventing cache problems, you can use:

    ldr.load(new URLRequest(url + "?rnd="+Math.random()));
    

    Now, this page might not give back the exact content of a site, but might be good enough to estimate the response time. With flash.

    So overall, this could clear the cache and load the site everytime to give a better result.