Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript 3 - checking for an internet connection

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?

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

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

Cheers

like image 787
angel13th Avatar asked Nov 26 '12 06:11

angel13th


2 Answers

(My reputation is too low to comment on Tianzhen Lin's answer directly ...)

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.

  • Changed:

    if( textReceived.indexOf( _contentToCheck ) )
    

    to:

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

    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.

  • Added:

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

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.

like image 176
David Guy Avatar answered Sep 28 '22 02:09

David Guy


I see two issues in your code. One is that while you have the logic to check internet connection, there is not any code calling the function, therefore the logic of redirection would not be called. Second is that using AIRcore.swc would be a bad idea as you have injected a dependency that might not work with or violate browser sandbox.

You may try the following approach which is tested and requires no AIR's SWC:

Step 1, include a new class ConnectionChecker as follow:

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

Step 2, in your main application or anywhere that internet connection should be checked, use the following snippet:

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
}
like image 21
Tianzhen Lin Avatar answered Sep 28 '22 03:09

Tianzhen Lin