Monday, April 29, 2002

Example : loading data from a data base into Flash MX with Flash Remoting and CF MX

Below is sample code that shows how to load data from a data base from Flash MX using Flash Remoting and ColdFusion components.

Download and install the ColdFusion MX preview release.

Download and install the Flash Remoting Addons for Flash MX.

You can download the files here.

First create a new ColdFusion component, name it DbTest.cfc and save it in wwwrootcommacromediadbtest

Here is the component code:

<cfcomponent>
 <cffunction name="getData" access="remote" returntype="query">
  <!-- CompanyInfo is a DSN name for an example DB that CFMX sets up by default -->
  <cfquery datasource="CompanyInfo" name="result">
    select * from Employee
  </cfquery>
  <cfreturn result />
 </cffunction>
</cfcomponent>

Open Flash, and place a Combo Box component on the stage, and give it an instance name of "dataBox"

add the following ActionScript to the first frame of the Flash movie:

#include "NetServices.as"
#include "NetDebug.as"
/** we will use an instance of the Result class to capture
 the data from the server **/
Result = function()
{
}
/** onResult is called when data is loaded from the server.
 In this case, a RecordSet object will be passed to it **/
Result.prototype.onResult = function(rs)
{
 trace("Data Received : " + rs.getLength() + " rows.");
 
 //data box is a simple combo box on the stage.
 dataBox.setDataProvider(rs);
}
/** onStatus is called if any errors occur when communicating
 with the server **/
Result.prototype.onStatus = function(error)
{
 trace("Error : " + error.description);
}
/** set the location of the Flash Remoting gateway **/
NetServices.setDefaultGatewayURL("http://localhost:8500/flashservices/gateway");
var gw = NetServices.createGatewayConnection();
/** get a reference to the service on the server
 First param is the path to the service on the server
 Second param is the object to send the server response too **/
var server = gw.getService("com.macromedia.dbtest.DbTest", new Result());
/** call the remote method **/
server.getData();


Save your Flash movie, and test. You should see all of the data displayed in the combo box. If it is formatted weird, then make the combo box wider.

If you get any errors, open the NetConnection Debugger (Windows > NetConnection Debugger) and then retest your movie. This will show all of the communication between Flash and the server.

This is just a simple example, but shows how easy it is to do some pretty complex stuff in Flash MX with Flash Remoting.

11:15:52 PM    comment []  Google It!  

Getting Started with ColdFusion MX and Flash Remoting : ServerSide ActionScript

This is an addendum to the following article:

Getting Started with ColdFusion MX and Flash Remoting article

that uses ServerSide ActionScript instead of a ColdFusion component.

1. open the above article in your web browser.

2. download and install the ColdFusion MX preview release.

3. Download and install the Flash Remoting Addons for Flash MX.

4. create HelloWorld.asr (the tutorial will tell you where) and add the following code:

function sayHello()
{
 return "Hello World";
}

open the example file in Flash MX and test your movie.

Of course, this is a really simple example. Play around with passing more complex data such as Arrays and RecordSets. Ill post more information later on how to make database calls, and load remote and local files into ServerSide ActionScript.

1:22:36 PM    comment []  Google It!  

Catching server timeout errors when using Flash Remoting

When calling remote services / methods via Flash Remoting, any errors that occur will trigger the onStatus method to be called:

onStatus = function(error)
{
     trace("Error : " + error.description);
}

However, if Flash cannot connect to the server (network or server is down) onStatus will not be called. Using XML and LoadVars you have to manually keep a timer in order to time out the connection, however you do not have to do this using Flash Remoting. Just create a method like the following:

_global.System.onStatus = function(info)
{
trace("details : " + info.details);
trace("description : " + info.description);
trace("code : " + info.code);
trace("level : " + info.level);
}

This method will be called if Flash MX cannot connect to the Flash Remoting server.

Here is an example output (when the server is not running):

details: http://localhost:8500/flashservices/gateway/
description: HTTP: Failed
code: NetConnection.Call.Failed
level: error

Couple of notes :

  • The exact messages may depend on the browser.
  • This will only works when connecting to the server via Flash Remoting. It will not work when using the XML or LoadVars object.
9:05:57 AM    comment []  Google It!  

© Copyright 2003 Mike Chambers.
 
April 2002
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Mar   May


Macromedia MX

Resources

Flash MX

Aggregators

Books

Click here to visit the Radio UserLand website.

Subscribe to "Examples" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.