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

查看:98
本文介绍了动作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();
} 

我正在尝试获取闪光灯以检查连接并导航至另一个

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而不是AIR应用程序的HTML页面

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

欢呼声

推荐答案

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

我需要制作一个进行一些更改以使Lin Lin zhen能够按预期工作:

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


  • 已添加:

  • Added:

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.)

通过上述更改,我发现Tianzhen的代码运行完美:无论我如何断开(或重新连接)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.

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

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