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 service—here, a CFC script—to request that the work be done by the back-end server and the results returned.

 
About the sample

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.

 
Re-creating the sample

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 ResultBox.

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 Run_btn, the label Run Tests, and the click handler runTests.

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_btn button. Notice the call to runTests. This invokes the function you'll define later in the server-side code. The second parameter, new Result(), provides the functionality for receiving the results of the call to runTests.

// 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.

// Handle the server's update of ResultBox with 
// the result of each test.
NetConnection.prototype.postResults = function(result) {
	_root.ResultBox.text += "\n" + result;
}

 
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 onAppStart function, define the location of the Flash Remoting service, and connect to it.

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 onConnect function, accept the client's connection.

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, testService. The second parameter, new TestResult(this), is a handler you'll define farther down in this file to catch the data coming back from 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 testService methods to get the results from the Flash Remoting methods getBoolean, getArray, and addNumbers, and close the function.

	// 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 TestResult function as a handler for data coming from the Flash Remoting service.

// Create a TestResult function
function TestResult(client) {
	this.client = client;
}

8

For each test, receive the result, result. Then call the client-defined function postResults and pass the client the result data.

// 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 [methodName]_Result callback convention from Flash Remoting. For example, for the call to getBoolean, you provide a call back to the getBoolean_Result method.

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 addNumbers method that the Flash Communication Server will invoke.

<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 getBoolean method that the Flash Communication Server will invoke.

	<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 getArray method that the Flash Communication Server will invoke.

	<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.