The
application is a simple widget that gets the current time
from the server and the IP address of your browser and returns
it to Macromedia Flash.
Server-side ColdFusion code
On the server side, you will build the backend ColdFusion
component. Open basic.cfc located inside the <webroot>\flashremoting\components\
folder. I suggest turning on line numbers in Dreamweaver
MX to follow along with the discussion (To turn on line
numbers, select View > Code > Code View Options >
Line numbers.)
This example calls the today()
function (see lines 14–19). The ColdFusion component
has a simple cfset tag
that builds the result and returns it with the cfreturn
tag.
Client-side Macromedia Flash ActionScript and Flash
Remoting code
For web application developers, looking at the Macromedia
Flash code can be a bit foreign, so I'm going to explain
it step-by-step. (Sorry about my lack of graphic design
skills; this tutorial's purpose is instructional, not design-oriented—you
designer types can fix this up for me.)
Open basicComponentUse.fla inside the Macromedia Flash
MX authoring environment. Two objects are on the stage:
a dynamic text box with the instance name of status
and a big red button with the instance name of trigger.
The button will execute an ActionScript function called
getData() when the client
presses it.
Select Frame 1 of the actions layer and open up the Actions
panel or press F9. Display the line numbers to help you
follow along as I explain the ActionScript code. (You can
turn on line numbers in the panel menu.)
The first few lines include two ActionScript libraries.
The first one, NetConnection.as, enables the application
to connect to the Flash Remoting service. The second one
enables the application to use the NetConnection debugger
to examine the code as it executes. While the NetConnection
Debugger is useful during development, you should remove
it in your final application, as it adds overhead to your
application.
Look at line 16:
NetServices.setDefaultGatewayUrl("http://localhost:8500/flashservices/gateway")
This is perhaps the single most important line of code:
It tells Macromedia Flash where to find the Flash Remoting
Gateway. If you did not install ColdFusion MX with its default
setup, you must modify the URL in line 16 so that it specifies
the location of your gateway.
The next line connects to the gateway and builds a connection
object named cfserver. Typically, I recommend
that you test your connection at this point to ensure that
you have the correct gateway URL. This could be difficutl
to verify without the NetConnection Debugger. It reports
communication between Macromedia Flash and the server.
Lastly, create an object inside the Macromedia Flash ActionScript—this
object represents the CFC on the ColdFusion server.
basic = cfServer.getService("flashremoting.components.basic",
this);
Now, a Macromedia Flash developer can refer to the basic
object, which contains all functions available in the basic.cfc,
which is on the ColdFusion server.
Using the NetConnection Debugger
If you open the NetConnection Debugger and test your movie
(Control > Test Movie or Control-Enter), you should see
the following.
|