Accessibility

Table of Contents

Integrating Remote Shared Objects with Flex and Flash Communication Server

Flash Communication Server

Flex and FCS create a client/server environment. Using the MXML code in the flexFCS_01.mxml file, Flex creates a client application that executes on a user’s machine. FCS acts as the server in this relationship, providing data and updates to the client.

This article is about interfacing two components, not programming FCS. For more on FCS check out the books mentioned in the beginning of the article. For now, I provide a basic server-side script to demonstrate the interface.

Place file, main.asc (included with the ZIP file in the Requirements section) on FCS server in a directory called random in the \Flash Communication Server MX\applications\ directory.

This file sets up a simple shared object on FCS server. The object has 10 slots, named 0-9. Each of these slots will have a random number. This random number changes every two seconds and FCS pushes the results to the client.

The Connection

At this point you have a simple client application, created in Flex, and a FCS application. Now you will make the client display the changing data from FCS. You accomplish this in two phases. First, you connect to FCS and the Remote Shared Object. Second, you receive information back from FCS and process it for display purposes.

First, add the following highlighted lines to Example 1 (flexFCS_01.mxml) or open the solution file, flexFCS_02.mxml, which is available from the fcs_flex_sample.zip that you downloaded at the beginning of this article.

Example 2: flexFCS_02.mxml (additions are highlighted)

<?xml version="1.0" encoding="utf-8"?> 

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" 				
		xmlns="*" 										      
		creationComplete="initializeApplication( event )"> 
 	
	<mx:Script> 
	
	<![CDATA[ 
		var myData:Array; 
		function initializeApplication( event ) 
		{ 
		//We are called by the creation complete event of the application tag  
		
		//Instantiate a new Array
		myData = new Array(); 
		
		//Connect to the flash communication server.
		//we are using rtmp as our first protocol attempt				
		my_fcs.connect('rtmp://[your_server]/random');
		
		} 
			 
	]]> 
	
	</mx:Script> 
	
	<mx:DataGrid id="display_grid" dataProvider="{myData}" width="90%" height="90%"/>
	 
	<!--Created a DataGrid and bound the dataProvider to myData--> 	
	<!--Instantiate an FCSService component-->
	<FCSService id="my_fcs"
		closed=" alert( 'server closed connection' )"					
		rejected=" alert('Server rejected connection')"
		failed=" alert('Server connection failed')" />
		
	<!--Instantiate an SharedRemote component and bind it to the FCSService tag above-->
	 
	<!--The name property of this tag must match the argument in the SharedObject.get command on the server file main.asc-->

	<SharedRemote id="my_remote1" 								   
	 	name="RandomNumbers"							 	   
		service="{my_fcs}"
		failed="alert('Shared Object Failed')"
		status="alert('Status ' + event.info.level + ' ' + event.info.code );"/>
</mx:Application> 

In this version, I added three important blocks of code:

  • An FCSService component
  • A SharedRemote component
  • A call to the FCSService object’s connect method

First, examine the FCSService component. The FCSService has an id of my_fcs, which you use to reference the instance going forward. You also defined event handlers for the closed, rejected, and failed events. If an end user encounters any type of problem, an alert box will display.

Second, the SharedRemote component has an id of my_remote1. The service property is set to {my_fcs}, the id of the FCSService component. This allows any FCSService component to have multiple shared remote objects defined. The SharedRemote component also defines event handlers for the failed and status messages. If an end user encounters a problem with the shared object, an alert box displays on the screen.

Finally, the initializeApplication handler calls my_fcs.connect( ‘rtmp://yourServerName/random’ ). The name, random, is the name of the application created when you placed the main.asc file into the random folder on your FCS.

When you invoke the connect method, you attempt to make a connection initially through RTMP to FCS. In reality, you actually have several protocols/port combinations which will be tried before a connection occurs. While this concept is outside the scope of this article, please read "Tunneling Macromedia Flash Communications Through Firewalls and Proxy Servers" for more information.

If you run the application at this point, you will see a DataGrid, much like the previous example.

If an alert box appears, ensure that your Flash Communication Server is running and that you placed the main.asc file in the proper directory as explained in the "Flash Communication Server" section.