Application Server Connectivity > Sample 1: Simple Remoting |
![]() ![]() ![]() |
Sample 1: Simple Remoting
In this sample, the client application requests some calculations to be done on the application server and the results to be returned in the string format. The client invokes calls to the Flash Communication Server. The Flash Communication Server, in turn, acts as a client of the Flash Remoting servicehere, a CFC scriptto request that the work be done by the back-end server and the results returned.
When the user clicks the Run Test button, the screen is updated with a message from the server, a Boolean value, an array of months, and the result of an addition operation.
The doc_remoting.fla file provides the user interface for testing connectivity between the client, the Flash Communication Server, and the ColdFusion MX server. The main.asc file accepts the connection from the client, and in turn, connects to ColdFusion using Flash Remoting. The simple.cfc file contains the scripts for getting a Boolean value, an array, and a sum.
Before you start to re-create the sample, see Creating your working environment.
To create the user interface for this sample:
1 |
From the toolbox, select the Text tool and draw a dynamic text box. In the Property inspector (Window > Properties), select Dynamic Text for the type of text box, and give it the instance name |
2 |
To add the button for running the tests, open the Components panel (Window > Components) and drag a push button onto the Stage. In the Property inspector, give it the instance name |
3 |
Save your file as doc_remoting.fla. |
4 |
Create a directory named doc_remoting in your Flash Communication Server applications directory. |
To write the client-side ActionScript for this sample:
1 |
Add the following debug code to trace all the status information coming back from the Flash Communication Server. |
// Trace all the status information. function traceStatus(info) { trace("Level: " + info.level + " Code: " + info.code); } // Create onStatus prototypes for the objects that // will return status. NetConnection.prototype.onStatus = traceStatus; NetStream.prototype.onStatus = traceStatus; SharedObject.prototype.onStatus = traceStatus; |
|
2 |
Create the event handler for the |
// Run the simple remoting tests. function runTests() { // Get the return value from the Flash Communication Server and // display it in the ResultBox. In this sample, the server // is returning the string "Ran the service on the server". function Result() { this.onResult = function(str) { _root.ResultBox.text = str; } } // Call the "runTests" function on the Flash Communication Server, // and pass in an instance of the Result function to receive // the return value from the server. main_nc.call("runTests", new Result()); } |
|
3 |
Create a new network connection and connect to the application. |
// Create a new connection and connect to the application // instance on the Flash Communication Server. main_nc = new NetConnection(); main_nc.connect("rtmp:/doc_remoting/room_01"); |
|
4 |
Provide a handler function that the Flash Communication Server can call to return the results of the tests. |
|
To write the server-side ActionScript for this sample:
1 |
Create a new file using your server-side ActionScript editor, and load the netservices.asc file to enable Flash Remoting through the Flash Communication Server. |
load("netservices.asc"); |
|
2 |
In the |
application.onAppStart = function() { trace("***** on app start"); // Set the default gateway URL NetServices.setDefaultGatewayUrl("http://localhost:8500/flashservices/gateway"); // Connect to the gateway this.gatewayConnection = NetServices.createGatewayConnection(); } |
|
3 |
In the |
application.onConnect = function (clientObj) { trace("***** on connect"); // Accept the connection for this client this.acceptConnection(clientObj); return true; } |
|
4 |
Provide a prototype function that the client can call to invoke the tests. |
Client.prototype.runTests = function() { trace("***** runTests"); |
|
5 |
In the same function, create an instance of the Flash Remoting service, |
// Get a reference to the service. "simple.simple" refers // to the ColdFusion file, simple.cfc, found in the "simple" // directory under the deployed CF directory. var testService = application.gatewayConnection.getService("simple.simple", new TestResult(this)); |
|
6 |
Call |
// Call the "testService" function on Flash Remoting // to get a Boolean, an array, and a sum. testService.getBoolean(true); testService.getArray(); testService.addNumbers(1,3); return "Ran the service on the server"; } |
|
7 |
Provide the |
// Create a TestResult function function TestResult(client) { this.client = client; } |
|
8 |
For each test, receive the result, |
// Get result of Boolean test and pass the result to the // client with a call to postResults TestResult.prototype.getBoolean_Result = function(result) { trace("***** getBoolean_Result: " + result); this.client.call("postResults", null, result); } // Get result of addition test and pass the result to the // client with a call to postResults TestResult.prototype.addNumbers_Result = function(result) { trace("***** addNumbers_Result: " + result); this.client.call("postResults", null, result); } // Get the array and pass it back to the client // with a call to postResults TestResult.prototype.getArray_Result = function(result) { trace("***** getArray_Result: " + result); this.client.call("postResults", null, result); } |
|
Note: In the samples in this chapter, you subscribe to the remoting service by including netservices.asc in your main.asc file. You need to provide a result method to handle any result value coming back from the remoting service in the main.asc file as well. The samples in this chapter use the |
|
9 |
Save the file as main.asc in the /applications/doc_remoting directory. |
To write the ColdFusion component for this sample:
1 |
Using your standard editor, create a new file and add code to name the application and indicate how to access it. |
<cfcomponent name="simple" access="remote"> |
|
2 |
Create the |
<cffunction name="addNumbers" output="false" description ="add two numbers" access="remote"> <cfargument name="num1" required="true" type="numeric" description="the first number"> <cfargument name="num2" required="true" type="numeric" description="the second number"> <cfreturn num1+num2> </cffunction> |
|
3 |
Create the |
<cffunction name="getBoolean" output="false" description="Returns a boolean that you specify." access="remote"> <cfargument name="bool" required="true" type="boolean" description="The boolean to return"> <cfreturn bool> </cffunction> |
|
4 |
Create the |
<cffunction name="getArray" output="false" description="Creates and returns an array of 3 items" access="remote"> <cfset months = ArrayNew(1)> <cfset months[1] = "January"> <cfset months[2] = "February"> <cfset months[3] = "March"> <cfreturn months> </cffunction> </cfcomponent> |
|
5 |
Save the file as simple.cfc in the simple directory under the directory where you publish all of your ColdFusion MX files. |
![]() ![]() ![]() |