0 Replies Latest reply on Jun 29, 2011 10:46 PM by Flex__RIA_Developer

    runtime check Internet connection

    Flex__RIA_Developer Level 1

      Hello..

       

      How can I check runtime internet connection is available or not in in Flex Web application.

       

      In Flex AIR application it is possible using following code

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                             xmlns:s="library://ns.adobe.com/flex/spark"
                             xmlns:mx="library://ns.adobe.com/flex/mx"
                             creationComplete="init()">
         
          <fx:Script>
              <![CDATA[
                 
                  /*
                  import air.net.ServiceMonitor;
                  import air.net.SocketMonitor;
                 
                  import flash.events.StatusEvent;
                 
                  socketMonitor = new SocketMonitor('www.adobe.com',6667);
                  socketMonitor.addEventListener(StatusEvent.STATUS, socketStatusChange);
                  socketMonitor.start();
                 
                  function announceStatus(e:StatusEvent):void {
                      trace("Status change. Current status: " + socketMonitor.available);
                  } */
                  // network monitoring functions
                  /* **************************** */
                 
                  // import the URLMonitor class
                  import air.net.URLMonitor;
                 
                  /*
                  Set up the URL string variable for the URLRequest to check.
                  In this example, let's use google.com as we can be sure 99.99%
                  of the time that it will be up and running.
                  */
                  private var strURLMonitor    : String = 'http://www.google.com';
                  private var    monitor            : URLMonitor;
                  [Bindable]
                  private var isOnline        : Boolean = false;
                 
                  /*
                  This function checks the online status by attempting
                  to resolve a connection to a remote address
                  */
                  private function monitorConnection():void {
                      monitor = new URLMonitor(
                          new URLRequest(strURLMonitor)
                      );
                      monitor.addEventListener(
                          StatusEvent.STATUS,
                          announceStatus);
                      monitor.start();
                  }
                 
                  /*
                  Declare the status from the monitorConnection function
                  and set the value to the variable isOnline
                  */
                  private function announceStatus(e:StatusEvent):void {
                      trace("Status change. Current status: " + monitor.available);
                      if(monitor.available) {
                          isOnline = true;
                          /*
                          Inside here, you can run any functions or script
                          that require network connection, as it exists.
                          */
                      } else {
                          isOnline = false;
                      }
                  }
                 
                  /*
                  The network connection has changed.
                  Run the monitorConnection function to check status
                  */
                  private function onNetworkChange(event:Event):void {
                      trace('network change');
                      monitorConnection();
                  }
                  //
                 
                  private function init():void {
                      /*
                      Keep an eye on network connectivity...
                      Any losses, and run the onNetworkChange method
                      */
                      NativeApplication.nativeApplication.addEventListener(
                          Event.NETWORK_CHANGE, onNetworkChange);
                     
                      // run monitorConnection
                      monitorConnection();
                  }
                 
              ]]>
          </fx:Script>
         
          <s:Label x="10"
                   y="10"
                   text="Online? {isOnline}"
                   width="493"
                   height="167"
                   fontSize="72" />
         
      </s:WindowedApplication>

       

       

      Thanks in Adavance.