use*_*482 5 actionscript-3 flash-cs6
我在我的flash文件中使用此代码
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();
}
Run Code Online (Sandbox Code Playgroud)
我试图让闪光灯检查连接并导航到另一个页面,如果不是它将重播.
它似乎没有工作.我错过了什么吗?
我还将库路径添加到aircore.swc.
它意味着是一个带有Flash而不是AIR应用程序的html页面
干杯
小智 7
(我的名声太低,不能直接评论田振林的答案......)
为了让Tianzhen Lin的答案符合预期,我需要做一些改动:
添加:
urlRequest.useCache = false;
urlRequest.cacheResponse = false;
Run Code Online (Sandbox Code Playgroud)
此添加是必需的,因为即使连接肯定丢失,检查仍然成功,因为正在读取缓存.
更改:
if( textReceived.indexOf( _contentToCheck ) )
Run Code Online (Sandbox Code Playgroud)
至:
if( !(textReceived.indexOf( _contentToCheck ) == -1) )
Run Code Online (Sandbox Code Playgroud)
此更改是必需的,因为始终找到""(空字符串)时,它在索引"0"处被找到,这导致原始if()条件始终失败.
添加:
urlRequest.idleTimeout = 10*1000;
Run Code Online (Sandbox Code Playgroud)
在网络电缆与路由器物理断开的情况下,请求可能需要很长时间才能超时(老实说,我已经厌倦了几分钟后等待.)
在做出上述更改后,我发现天臻的代码完美无缺:无论我如何断开/重新连接Wi-Fi(在iOS和Android设备上),始终检测到连接状态的变化.
我在你的代码中看到了两个问题.一个是当你有逻辑来检查互联网连接时,没有任何代码调用该函数,因此不会调用重定向的逻辑.其次是使用AIRcore.swc是一个坏主意,因为你注入了一个可能无法使用或违反浏览器沙箱的依赖项.
您可以尝试以下方法进行测试,不需要AIR的SWC:
第1步,包括一个新课程ConnectionChecker如下:
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 ) );
}
}
}
Run Code Online (Sandbox Code Playgroud)
第2步,在主应用程序或应检查互联网连接的任何地方,使用以下代码段:
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
}
Run Code Online (Sandbox Code Playgroud)