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

I need to ping a website with ActionScript or Animate

我找到了这段代码 ( 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();
}

嗯,它正在尝试加载网站 10 次并检查响应。不过我会摆脱这个限制,连续尝试 10 次是没有意义的。

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

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

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