我需要使用ActionScript或Animate ping一个网站

时间:2018-03-05 22:40:06

标签: actionscript-3 flash adobe

我找到了这段代码(I need to ping to an network with flash or actionscript) 我不确定我是理解它还是如何使用它 - 我需要将其重定向到另一个框架,如果它工作,另一个框架,如果它失败

有人能帮忙吗?

    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();
}

1 个答案:

答案 0 :(得分:2)

好吧,它试图加载一个网站10次并检查响应。我会摆脱限制,连续10次尝试它是没有意义的。

不要忘记您的服务器上需要一个crossdomain.xml文件才能从闪存中访问它。

您需要为您的目的添加更多内容:

// add an event listener for a failed call
ldr.addEventListener(IOErrorEvent.IO_ERROR, ldrFailed);
ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR , ldrSecurityError);

function ldrFailed(evt:*):void
{   
    // loader failed (no internet connection), try to ping again
    doFailedRedirect();
}

function ldrSecurityError(evt:*):void
{   
    // There is an internet connection but probably something is wrong with your crossdomain.xml
    doFailedRedirect();
}

function doRedirect():void
{   
    // make your redirect here
}

function doFailedRedirect():void
{   
    // something went wrong, do your failed redirect here
}

调整ldrStatus函数:

function ldrStatus(evt:*):void
{
    if(evt.status == 200)
    {
        // server responded with status 200 wich means everything is fine 
        time_stop = getTimer();
        trace("got response in", time_stop - time_start, "ms");
        doRedirect();
    }
    else
    {
        // there is an internet connection but the server returns something else (probably something is wrong with the server)
        doFailedRedirect();
    }
}