Actionscript 3 - 检查互联网连接 [英] Actionscript 3 - checking for an internet connection

查看:32
本文介绍了Actionscript 3 - 检查互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Flash 文件中使用此代码

I am using this code in my flash file

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

我正在尝试让 Flash 检查连接并导航到另一个页面,否则它将重播.

I am trying to get the flash to check for a connection and navigate to another page if not it will replay.

它似乎不起作用.我错过了什么吗?

It doesnt seem to be working. Am i missing anything?

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

I have add the library path to aircore.swc also.

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

Its meant to be a html page with flash rather than an AIR app

干杯

推荐答案

(我名气太低,无法直接评论林天真的回答...)

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

I needed to make a few changes in order to get Tianzhen Lin's answer to work as expected:

  • 添加:

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

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

This addition was required because even when the connection was definitely lost, the check was still succeeding because the cache was being read from.

改变:

if( textReceived.indexOf( _contentToCheck ) )

到:

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

需要进行此更改,因为虽然始终可以找到 ""(一个空字符串),但它是在索引 '0' 处找到的,这导致原始 if() 条件始终失败.

This change was required because while "" (an empty string) was always being found, it was being found at index '0' which was causing the original if() condition to always fail.

添加:

urlRequest.idleTimeout = 10*1000;

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

In the case where the network cable was physically disconnected from the router, the request could take a very long time to time-out (Honestly, I got tired of waiting after a couple of minutes.)

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

After making the above changes, I found Tianzhen's code worked perfectly: No matter how I went about disconnecting/reconnecting Wi-Fi (on both iOS and Android devices) the change in connection status was always detected.

这篇关于Actionscript 3 - 检查互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆