Flash CS6 - AS3 - 检查互联网连接不工作 [英] Flash CS6 - AS3 - Check internet connection not working at all

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

问题描述

我有一个Adobe AIR应用程序,可以在IOS和Android上运行。我已经搜索了所有的互联网,看看我如何检查设备是否连接到工作连接或没有。最合乎逻辑的方法是:
创建一个ConnectionChecker类,其内容如下:

  package 
{
import flash.events。*;
import flash.net。*;
$ b $ [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;

//虽然google.com可能是一个想法,但通常使用具有已知内容的网址(例如http://foo.com/bar /mytext.txt
//通过这样做,已知内容也可以被验证。

//这将使检查更可靠,因为无线热点登录
//页面会负面影响结果。
private var _urlToCheck:String =http://www.google.com;


//空字符串,所以它总是正面的
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);

尝试
{
loader.load(urlRequest);

catch(e:Error)
{
dispatchErrorEvent();


$ b私有函数loader_complete(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
var textReceived:String = loader.data as String; (textReceived)
{
if(textReceived.indexOf(_contentToCheck))
{
dispatchSuccessEvent();

if
}
else
{
dispatchErrorEvent();
}
}
else
{
dispatchErrorEvent();


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


$ b 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;
//有互联网连接


private function checker_error(event:Event):void
{
trace(U d''t have CNX);
continueTheGame.visible = false;
//没有互联网连接
}

以上所有答案都是基于这个链接我发现在SO https://stackoverflow.com/a/13569775/5128351



现在的问题是,无论我是否有连接或不,我总是会得到没有连接可用的结果。我是否错过了什么?



编辑:



我尝试添加以下内容到xml文件,但我仍然得到相同的结果:

 < key> NSAppTransportSecurity< / key> 
< dict>
< key> NSExceptionDomains< / key>
< dict>
< key> google.com< / key>
< dict>
<! - 包含以允许子域名 - >
< key> NSIncludesSubdomains< / key>
< true />
<! - 包含以允许HTTP请求 - >
< key> NSTemporaryExceptionAllowsInsecureHTTPLoads< / key>
< true />
<! - 包含指定最小TLS版本 - >
< key> NSTemporaryExceptionMinimumTLSVersion< / key>
< string> TLSv1.1< / string>
< / dict>
< / dict>
< / dict>


解决方案

关于 _contentToCheck 字符串


//空字符串总是正数

private var _contentToCheck:String =;


你在那个加载的HTML源文件中检查实际的target文本。这样,如果存在的话,至少可以确定Google已经加载好了(设备有连接)。

$ $ p $ $ code确认网页加载成功的HTML字符串
private var _contentToCheck:String =<!doctype html>; //开始Google的HTML文本



关于<$ c如果在 loader_complete中使用类似下面的逻辑,会发生什么情况?$ c $> $ check>() function 事件...?

 私有函数loader_complete(event:Event):void 
{
var loader:URLLoader = URLLoader(event.target);
var textReceived:String = loader.data as String;

if(textReceived.length> 0)
{
//如果找不到搜索文本,index将为-1(使用IS NOT EQUAL来检查)
if(textReceived.indexOf(_contentToCheck)!= -1)
{
dispatchSuccessEvent();
}
else
{
dispatchErrorEvent();
}
}
else
{
dispatchErrorEvent();




$ b代码不是编译器测试的,但逻辑应该是好的为了你自己的考验希望能帮助到你。 使用代码示例编辑 : $

$ b

$ b

b
$ b

以下是我测试过的两个代码(正确检测网络连接的开/关)。
重新启用函数 loader_complete 来检查任何东西。



(1) 连接到 Connect_test_01。作为(是主要的AS类)

 
{
import flash .display.MovieClip;
导入flash.events。*;

导入ConnectionChecker;

public class Connect_test_01扩展MovieClip
{
public var checker:ConnectionChecker;

公共函数Connect_test_01()
{
checker = new ConnectionChecker();
checker.addEventListener(ConnectionChecker.EVENT_SUCCESS,checker_success);
checker.addEventListener(ConnectionChecker.EVENT_ERROR,checker_error);
checker.check();

$ b $ private function checker_success(event:Event):void
{
//#有互联网连接
trace( - YES - 网络发现..你连接);
//continueTheGame.visible = true;

$ b $ private function checker_error(event:Event):void
{
//#没有互联网连接
trace( - NO - 未找到网络...无连接);
//continueTheGame.visible = false;
}


} // end Class

} // end Package

(2)这是以上代码导入中使用的 ConnectionChecker.as / p>

 
{
import flash.events。*;
import flash.net。*;
$ b $ [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 $ b $ private var _urlToCheck:String =https://www.google.com;

//检查HTML内容中的字符串以确认网页加载成功
private var _contentToCheck:String =<!doctype html>; //开始Google的HTML文本

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

$ b private function loader_complete(event:Event):void
{
loaderB = URLLoader(event.target);
textReceived = String(loaderB.data); //作为字符串;
$ b $ //检查加载内容(应该是HTML源代码)
// trace(STRING CHECK:+\\\
+ textReceived +\\\
);

//#minus-1表示未找到。搜索文本开始时应该为零
// trace(Index of String:+ textReceived.indexOf(_contentToCheck)+\\\
);

if(textReceived.length> 0)
{
//如果找不到搜索文本,index将为-1(使用IS NOT EQUAL来检查)
if(textReceived.indexOf(_contentToCheck)!= -1)
{dispatchSuccessEvent(); }
else {dispatchErrorEvent(); }
}
else
{dispatchErrorEvent(); }


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

$ b private function dispatchSuccessEvent():void
{dispatchEvent(new Event(EVENT_SUCCESS)); }
$ b private function dispatchErrorEvent():void
{dispatchEvent(new Event(EVENT_ERROR)); }
}
}


I have an adobe air application working on both IOS and Android. I have searched all over the internet to see how can i check whether the device is connected to a working connection or not. The most logical way was the following :
Create a ConnectionChecker class with the following content :

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

Afterwards, whenever i need to actually test the presence of a working connection, i'd do the following :

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

And the appropriate functions are :

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
}

All the above answer is based on this link i found on SO https://stackoverflow.com/a/13569775/5128351

Now the problem is, no matter whether i have connection or no, i will always get the result that no connection available. Am i missing something??

EDIT:

I tried to add the following to the xml file, but i'm still getting the same result :

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

解决方案

Regarding _contentToCheck String :

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

I would suggest you check for actual "target" text within that loaded HTML source. This way, if it exists, you can be sure at least Google was loaded O.K (device has connection).

// check string of HTML to confirm web load was successful
private var _contentToCheck:String = "<!doctype html>"; //beginning of Google's HTML text


Regarding Check() function

What happens if you use logic like below in your loader_complete event...?

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

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

Code is not compiler tested but the logic should be good for your own test. Hope it helps.


EDIT with code sample:

Below is the two codes I tested (which correctly detects On/Off internet connection).
Re-enable those traces in function loader_complete to double-check anything.

(1) FLA is linked to this Connect_test_01.as (is main AS class)

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

(2) This is the ConnectionChecker.as used in above code's imports.

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 CS6 - AS3 - 检查互联网连接不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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