Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...