Test for Internet Connection


One of the first major tasks the application needs to accomplish is to detect whether an Internet connection is available. For this, the Vshift ColdFusion team has built a simple CFC function called onlineTest that will be placed in the expenseapp.cfc and will be called from Flash to test if there is a connection. If Flash cannot connect to the server and retrieve the result, then the user is obviously not connected properly to the Internet.

 <cffunctionname="onlineTest"returntype="boolean"  access="remote"  hint="Simplyreturnsavaluefortesting"> <cfreturntrue> </cffunction> 

This RETURNTYPE property tells the function that it will be returning a Boolean value, and ACCESS="REMOTE" allows remote servers to access this function. The hint property provides a visual clue as to the use of the function to remote services that may try to access this function without knowing what it actually does.

For the Flash part of this application, the code needs to be written to ask for a user ID and password if the user is online. If the user is not online, the application should only ask for the user ID (since the user will not be logging in to the application). The Flash team will also add a graphical visual cue in the form of a connection light that will turn green when the user is connected.

To accomplish this, the team has created some ActionScript in the log-in section of the Flash application that includes the NetServices.as files, as well as an initialization call that will connect to the gateway and try to call a CFC function.

 //includeNetservicesandNetdebug  #include"NetServices.as" #include"NetDebug.as" //callinitfunction maininit(); functionmaininit(){  //setglobalonlinevariabletofalse  _global.online=false;  //doalloweditingoffieldsyet  login_form.usernameField.type="dynamic";  login_form.passwordField.type="dynamic";  //connecttonetservicesgateway  NetServices.setDefaultGatewayUrl("http://www. vshift.com:8500/flashservices/gateway");  gatewayConnection=NetServices.createGatewayConnection();  //setCFCServiceequaltotheexpenseapp.cfcandrunthe onlinetest  CFCService= gatewayConnection.getService("com.monkeytongue.expenseapp ",this);  CFCService.onlineTest(); } 

Starting with the beginning of the maininit() function, the code first sets a global variable called online to false.

Next the application changes the text box types for the usernameField and the passwordField on the loginForm movie clip. Changing them to the type"dynamic" makes it so the user cannot interact with them.

instant message

The _global variable scope is new to Flash MX and gives us the ability to store information in a variable that can be accessed from any movie clip.


The next couple of lines of code connect to the gateway and set a variable named CFCService to point to the expenseapp.cfc. Now the functions of expenseapp.cfc are accessed by calling CFCService.functionname(). In the code above, the onlineTest function is called.

In Flash MX, successful component function calls automatically call a function named functionname_result (in the example, this would be onlineText_result). So the team has simply added a function called onlineTest_result, which will be called automatically.

instant message

In Flash MX the TextField object has been expanded to allow much more control over it. The type property is just one example of these new features; you can find more in the ActionScript Dictionary under "TextField."


 functiononlineTest_Result(result){   //iftestreturnsaresult,allowformfieldstobeedited  login_form.usernameField.type="input";  login_form.passwordField.type="input";  //setglobalonlinevariabletotrue  _global.online=true;  //andgotochangethestateofthestatusmovieclipto"online"  //thisshowsthegreencircleinsteadoftheyellowone  login_form.status_mc.gotoAndStop("online"); } 

This function changes the form fields on login_form back to the type "input", which makes them accessible by the end user. Also the global variable online is set to true. Finally, the status_mc movie clip, which resides on the login_form movie clip, is advanced to the Online frame (this changes a graphic to show that we have successfully connected to the Internet).

Finally, the team will add a special function to the ActionScript, which is specifically utilized to check for NetConnection errors.

 //thisfunctionchecksformajornetconnectionerrors  _global.System.onStatus=function(info){  //ifnoconnectionestablishedonlyallowusernametobeentered  login_form.usernameField.type="input";  //setglobalonlinevariabletofalse  _global.online=false;  //makesurethestatusmovieclipshowstheorangecircle  login_form.status_mc.gotoAndStop("offline"); } 

This function will only run if there is a serious connection error with the NetConnection. When called, it changes just the usernameField to an "input" type and sets the globalonline variable to false. The function also changes the status_mc movie clip to display a graphic, which shows that we are offline.



Reality Macromedia ColdFusion MX. Macromedia Flash MX Integration
Reality Macromedia ColdFusion MX: Macromedia Flash MX Integration
ISBN: 0321125150
EAN: 2147483647
Year: 2002
Pages: 114

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net