Actionscript 3 Flash CS6-AS3-检查互联网连接是否完全不工作
我有一个在IOS和Android上运行的AdobeAIR应用程序。我在互联网上搜索过,想知道如何检查设备是否连接到工作连接。最合乎逻辑的方式是:Actionscript 3 Flash CS6-AS3-检查互联网连接是否完全不工作,actionscript-3,flash,air,Actionscript 3,Flash,Air,我有一个在IOS和Android上运行的AdobeAIR应用程序。我在互联网上搜索过,想知道如何检查设备是否连接到工作连接。最合乎逻辑的方式是: 使用以下内容创建ConnectionChecker类: package { import flash.events.*; import flash.net.*; [Event(name="error", type="flash.events.Event")] [Event(name="success", type="f
使用以下内容创建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 ) );
}
}
}
之后,每当我需要实际测试工作连接的存在性时,我都会执行以下操作: 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
{
trace("U have cnx");
continueTheGame.visible = true;
// There is internet connection
}
private function checker_error(event:Event):void
{
trace("U dn't have cnx");
continueTheGame.visible = false;
// There is no internet connection
}
以上所有答案都是基于我在SO上找到的这个链接
现在的问题是,无论我是否有连接,我总是会得到没有连接可用的结果。我错过了什么吗
编辑:我试图将以下内容添加到xml文件中,但仍然得到相同的结果:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<!--Include to allow subdomains-->
<key>NSIncludesSubdomains</key>
<true/>
<!--Include to allow HTTP requests-->
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<!--Include to specify minimum TLS version-->
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
谷歌网站
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
NSTemporaryExceptionMinimumTLSVersion
TLSv1.1
关于\u contentToCheck
String:
//空字符串,因此它将始终为正private var_contentToCheck:String=”“代码>
我建议您检查加载的HTML源中的实际“目标”文本。通过这种方式,如果它存在,您可以确定至少谷歌加载了OK(设备有连接)
代码没有经过编译器测试,但逻辑应该适合您自己的测试。希望能有帮助
使用代码示例编辑:
下面是我测试过的两个代码(可以正确检测互联网连接的开/关)。
在函数loader\u complete
中重新启用这些跟踪,以重复检查任何内容
(1)FLA链接到此连接测试\u 01。as
(主要as类)
(2)这是连接检查程序。如上述代码导入中使用的
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 var urlRequest:URLRequest;
public var loaderA:URLLoader;
public var loaderB:URLLoader;
public var textReceived:String;
public static const EVENT_SUCCESS:String = "success";
public static const EVENT_ERROR:String = "error";
// URL to connect for testing
private var _urlToCheck:String = "https://www.google.com";
// check string within HTML content to confirm web load was successful
private var _contentToCheck:String = "<!doctype html>"; //beginning of Google's HTML text
public function ConnectionChecker()
{ super(); }
public function check():void
{
urlRequest = new URLRequest(_urlToCheck);
loaderA = new URLLoader();
loaderA.dataFormat = URLLoaderDataFormat.TEXT;
loaderA.addEventListener(Event.COMPLETE, loader_complete);
loaderA.addEventListener(IOErrorEvent.IO_ERROR, loader_error);
try { loaderA.load(urlRequest); }
catch ( e:Error ) { dispatchErrorEvent(); }
}
private function loader_complete(event:Event):void
{
loaderB = URLLoader( event.target );
textReceived = String(loaderB.data); // as String;
//# check contents of load (should be HTML source-code)
//trace ("STRING CHECK : " + "\n" + textReceived + "\n");
//# minus-1 means Not Found. Should be Zero since Search Text is beginning
//trace("Index of String : " + textReceived.indexOf( _contentToCheck ) + "\n" );
if ( textReceived.length > 0 )
{
//index will be -1 if search text is not found (use IS NOT EQUAL to check this)
if ( textReceived.indexOf( _contentToCheck ) != -1 )
{ 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 ) ); }
}
}
包
{
导入flash.events.*;
导入flash.net。*;
[事件(name=“error”,type=“flash.events.Event”)]
[事件(name=“success”,type=“flash.events.Event”)]
公共类ConnectionChecker扩展了EventDispatcher
{
公共变量urlRequest:urlRequest;
public-var-loaderA:URLLoader;
公共变量加载器b:URLLoader;
公共变量textReceived:String;
公共静态常量事件\u成功:String=“成功”;
公共静态常量事件\u错误:String=“ERROR”;
//要连接以进行测试的URL
私有变量_urlToCheck:String=”https://www.google.com";
//检查HTML内容中的字符串以确认web加载成功
private var _contentToCheck:String=”“//谷歌HTML文本的开头
公共函数连接检查器()
{super();}
公共函数检查():void
{
urlRequest=新的urlRequest(\u urlToCheck);
loaderA=新的URLLoader();
loaderA.dataFormat=URLLoaderDataFormat.TEXT;
loaderA.addEventListener(Event.COMPLETE,loader_COMPLETE);
loaderA.addEventListener(IOErrorEvent.IO_ERROR,loader_ERROR);
请尝试{loaderA.load(urlRequest);}
catch(e:Error){dispatchErrorEvent();}
}
私有函数加载程序_完成(事件:事件):无效
{
loaderB=URLLoader(event.target);
textReceived=String(loaderB.data);//作为字符串;
//#检查加载内容(应为HTML源代码)
//跟踪(“字符串检查:“+”\n“+textReceived+”\n”);
//#减-1表示未找到。应为零,因为搜索文本正在开始
//跟踪(“字符串索引:+textReceived.indexOf(\u contentToCheck)+”\n”);
如果(textReceived.length>0)
{
//如果未找到搜索文本,索引将为-1(使用不等于检查此项)
if(textReceived.indexOf(\u contentToCheck)!=-1)
{dispatchSuccessEvent();}
else{dispatchErrorEvent();}
}
其他的
{dispatchErrorEvent();}
}
私有函数加载器_错误(事件:IOErrorEvent):无效
{dispatchErrorEvent();}
私有函数dispatchSuccessEvent():void
{dispatchEvent(新事件(事件成功));}
私有函数dispatchErrorEvent():void
{dispatchEvent(新事件(事件错误));}
}
}
Adobe创建了一个URLMonitor类来处理此问题
您是否已将现在所需的NSTransportSecurity XML添加到iOS应用程序描述符中?您需要指定要与之通信的所有域
这进入iPhone节点内部
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>another domain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
NSAppTransportSecurity
NSExceptionDomains
谷歌网站
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
另一个域.com
n包括多个域
NSTemporary ExceptionalLowsInSecureHttpLoads
仅限空气??太神了不管怎样,这个类不是实现了与Asker的AS3代码相同的功能吗?例如:checkStatus()
“试图从后台的URL加载内容,以检查返回的HTTP状态代码。”-Asker的问题是试图通过检查空字符串==进行验证,但无效!!如果他的check函数返回一个数字(即:200
或404
),他将重新创建该URLMonitor。他显示的代码在浏览器或其他非空中情况下也能工作。我会在几个小时后检查它,然后再给你回复,你的答案是有意义的!还是一样,我一直没有联系,你建议@VC.one该代码适合我。我已经添加了一个我尝试过的代码示例。只是重新整理了一些东西(例如:m
package
{
import flash.display.MovieClip;
import flash.events.*;
import ConnectionChecker;
public class Connect_test_01 extends MovieClip
{
public var checker:ConnectionChecker;
public function Connect_test_01()
{
checker = 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
trace("-- YES - Net found.. You are connected");
//continueTheGame.visible = true;
}
private function checker_error(event:Event):void
{
//# There is no internet connection
trace("-- NO - Net not found... No connection");
//continueTheGame.visible = false;
}
} //end Class
} //end Package
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 var urlRequest:URLRequest;
public var loaderA:URLLoader;
public var loaderB:URLLoader;
public var textReceived:String;
public static const EVENT_SUCCESS:String = "success";
public static const EVENT_ERROR:String = "error";
// URL to connect for testing
private var _urlToCheck:String = "https://www.google.com";
// check string within HTML content to confirm web load was successful
private var _contentToCheck:String = "<!doctype html>"; //beginning of Google's HTML text
public function ConnectionChecker()
{ super(); }
public function check():void
{
urlRequest = new URLRequest(_urlToCheck);
loaderA = new URLLoader();
loaderA.dataFormat = URLLoaderDataFormat.TEXT;
loaderA.addEventListener(Event.COMPLETE, loader_complete);
loaderA.addEventListener(IOErrorEvent.IO_ERROR, loader_error);
try { loaderA.load(urlRequest); }
catch ( e:Error ) { dispatchErrorEvent(); }
}
private function loader_complete(event:Event):void
{
loaderB = URLLoader( event.target );
textReceived = String(loaderB.data); // as String;
//# check contents of load (should be HTML source-code)
//trace ("STRING CHECK : " + "\n" + textReceived + "\n");
//# minus-1 means Not Found. Should be Zero since Search Text is beginning
//trace("Index of String : " + textReceived.indexOf( _contentToCheck ) + "\n" );
if ( textReceived.length > 0 )
{
//index will be -1 if search text is not found (use IS NOT EQUAL to check this)
if ( textReceived.indexOf( _contentToCheck ) != -1 )
{ 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 ) ); }
}
}
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>google.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
<key>another domain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>