5

我在我的闪存文件中使用此代码

import air.net.URLMonitor;
import flash.net.URLRequest;
import flash.events.StatusEvent;

var monitor:URLMonitor;

function checkInternetConnection(e:Event = null):void
{
var url:URLRequest = new URLRequest("http://www.google.com");
url.method = "HEAD";
monitor = new URLMonitor(url);
monitor.pollInterval = 1000;
//
monitor.addEventListener(StatusEvent.STATUS, checkHTTP);
//
function checkHTTP(e:Event = null):void
{
if (monitor.available) {

       navigateToURL(new URLRequest("flickr.html"),"_top"); 

   } else {

       gotoAndPlay(1);

   }
}
monitor.start();
} 

我试图让闪光灯检查连接并导航到另一个页面,如果没有它会重播。

它似乎没有工作。我错过了什么吗?

我也将库路径添加到 aircore.swc。

它应该是一个带有 flash 的 html 页面,而不是一个 AIR 应用程序

干杯

4

3 回答 3

7

(我声望太低,无法直接评论林天真的回答……)

我需要进行一些更改才能使林天真的答案按预期工作:

  • 添加:

    urlRequest.useCache = false;
    urlRequest.cacheResponse = false;
    

    此添加是必需的,因为即使连接肯定丢失,检查仍然成功,因为正在读取缓存。

  • 改变:

    if( textReceived.indexOf( _contentToCheck ) )
    

    至:

    if( !(textReceived.indexOf( _contentToCheck ) == -1) )
    

    之所以需要此更改,是因为虽然总是找到“”(空字符串),但在索引“0”处找到它,这导致原始 if() 条件始终失败。

  • 添加:

    urlRequest.idleTimeout = 10*1000;
    

    在网络电缆与路由器物理断开连接的情况下,请求可能需要很长时间才能超时(老实说,几分钟后我已经厌倦了等待。)

进行上述更改后,我发现天真的代码运行良好:无论我如何断开/重新连接 Wi-Fi(在 iOS 和 Android 设备上),始终检测到连接状态的变化。

于 2013-09-25T22:57:41.430 回答
6

I see two issues in your code. One is that while you have the logic to check internet connection, there is not any code calling the function, therefore the logic of redirection would not be called. Second is that using AIRcore.swc would be a bad idea as you have injected a dependency that might not work with or violate browser sandbox.

You may try the following approach which is tested and requires no AIR's SWC:

Step 1, include a new class ConnectionChecker as follow:

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

    [Event(name="error", type="flash.events.Event")]
    [Event(name="success", type="flash.events.Event")]
    public class ConnectionChecker extends EventDispatcher
    {
        public static const EVENT_SUCCESS:String = "success";
        public static const EVENT_ERROR:String = "error";

        // Though google.com might be an idea, it is generally a better practice
        // to use a url with known content, such as http://foo.com/bar/mytext.txt
        // By doing so, known content can also be verified.

        // This would make the checking more reliable as the wireless hotspot sign-in
        // page would negatively intefere the result.
        private var _urlToCheck:String = "http://www.google.com";


        // empty string so it would always be postive
        private var _contentToCheck:String = "";    

        public function ConnectionChecker()
        {
            super();
        }

        public function check():void
        {
            var urlRequest:URLRequest = new URLRequest(_urlToCheck);
            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;

            loader.addEventListener(Event.COMPLETE, loader_complete);
            loader.addEventListener(IOErrorEvent.IO_ERROR, loader_error);

            try
            {
                loader.load(urlRequest);
            }
            catch ( e:Error )
            {
                dispatchErrorEvent();
            }
        }

        private function loader_complete(event:Event):void
        {
            var loader:URLLoader = URLLoader( event.target );
            var textReceived:String = loader.data as String;

            if ( textReceived )
            {
                if ( textReceived.indexOf( _contentToCheck ) )
                {
                    dispatchSuccessEvent();
                }
                else
                {
                    dispatchErrorEvent();
                }
            }
            else
            {
                dispatchErrorEvent();
            }
        }

        private function loader_error(event:IOErrorEvent):void
        {
            dispatchErrorEvent();
        }


        private function dispatchSuccessEvent():void
        {
            dispatchEvent( new Event( EVENT_SUCCESS ) );
        }

        private function dispatchErrorEvent():void
        {
            dispatchEvent( new Event( EVENT_ERROR ) );
        }
    }
}

Step 2, in your main application or anywhere that internet connection should be checked, use the following snippet:

var checker:ConnectionChecker = new ConnectionChecker();
checker.addEventListener(ConnectionChecker.EVENT_SUCCESS, checker_success);
checker.addEventListener(ConnectionChecker.EVENT_ERROR, checker_error);
checker.check();

private function checker_success(event:Event):void
{
    // There is internet connection
}

private function checker_error(event:Event):void
{
    // There is no internet connection
}
于 2012-11-26T17:15:02.880 回答
0

air.net.URLMonitor 是 AIR 可用的类 - 所以不能在 AIR 播放器之外工作。

但是你可以尝试自己做,因为这个监视器类所做的只是“ping”url并检查响应,所以你可以做类似的尝试从google.com等已知来源加载一些东西,如果它在没有错误的情况下完成,那么它是好的,否则你会得到错误。

于 2012-11-26T07:37:38.980 回答