I am creating a local app that is going to be a swf file. Is there anyway to check if the user has internet while they are using my app. I tried this, it works great if testing the movie from flash, however, It does not work when running the swf file independently. I'm guessing this is because it is strictly intended for air app.
What i need is something like:
var isNet:Boolean;
setInterval(checkNet, 1000);开发者_StackOverflow社区
function checkNet():void{
if(netAvailable)
isNet=true;
}else{
isNet=false;
}
So, that it starts of as true when my network wire is connected, and when I unplug it it sets it to false.
Is any way I can do this?
*I have tried putting a small xml file on a webserver and loading it with urlrequest with no luck.
Thank you for all the help.
Check out this question on StackOverflow; it may help. You could use this to check availability of a general IP address, like 4.2.2.2 or 8.8.8.8 (Google DNS).
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, cbGetCardData );
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler );
var request:URLRequest;
request = new URLRequest( 'http://myDomain.com/someFileOnYourServer.html' );
request.method = URLRequestMethod.POST;
try {
urlLoader.load( request );
} catch (e:Error) {trace(e);}
private function ioErrorHandler( e:IOErrorEvent ):void{
trace('This should fire if no network connection' )
}
private function securityErrorHandler( e:Event ):void{
trace('securityErrorHandler')
}
For Air you can use air.net.URLMonitor to check your network connection.
import air.net.URLMonitor;
import flash.net.URLRequest; import flash.events.StatusEvent;
var testURL:String = "http://www.google.com"; var testRequest:URLRequest = new URLRequest(testURL); var urlCheck:URLMonitor = new URLMonitor(testRequest); urlCheck.addEventListener(StatusEvent.STATUS, statusChanged); urlCheck.start();
function statusChanged(event:StatusEvent) { trace("my status is: "+urlCheck.available); }
精彩评论