Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: Adobe Flex

Adobe Flex: Article

Integrating Remote Shared Objects with Flex and Flash Communication Server

In this project, a remote shared object on a Flash Communication Server contains data that changes frequently

Example 3: flexFCS_03.mxml (additions shown in bold):
<?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');
}
function syncData( event )
{
var currentIndex:Number;
var currentNode:Object;
for ( var i=0; i<event.actions.length; i++ ) {

/* This is cheap and cannot be relied upon, however, for this example: our slot 'names' are all
numeric and can be used as Indexes into the array.These names are set when we do the
random_so.setProperty("0", ... ); in the main.asc file.The "0" is the slot name So, in this
case the slots are named 0,1,2,3 and so on. */
currentNode = event.actions[i];
/* Whenever we receive a synchronization event from FCS, it is provided as an
array.Each element in the array has a code, which defines the type of event we are
receiving. In this case, we are only looking at two of the possibilities, a slot changed or
the whole array should be cleared */ switch (currentNode.code)
{
case "change" :
//This slot has changed and needs to be
//updated

if ( myData[ currentNode.name ] == undefined )
{ //Even though FCS sees this data as
//a change, this is the first time we have
//data in the slot
//So we add it to our data structure
myData.addItemAt( currentNode.name,
{ label:event.data[ currentNode.name ].label,
value:event.data[ currentNode.name ].value } );
//Notice above that we are using the { } as a shortcut to create a new object from the
data we received from FCS
//This is do to the databinding properties of flex.
//If we do not create a copy of the object, flex will try to update FCS server
everytime we make a change to the object
//This can be desireable, but, it is not for this example
}
else
{
//We have seen data for this slot before, so we update our data structure
myData.replaceItemAt( currentNode.name,
{ label:event.data[ currentNode.name ].label,
value:event.data[ currentNode.name ].value } );
}
break;
case "clear" :
//FCS has instructed us to clear all of our existing data
myData.removeAll();
break;
}
}
}
]]>
</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 );"
sync="syncData( event )"/>
</mx:Application>

In this version, you added two important items. You specify an event handler for the sync event of the SharedRemote component and added the code for that handler.

First, let's talk about the syncData function. This is the function that performs all of the work in preparing the data for display. The event object passed to this function always contains two very important properties: actions and data.

The actions property is an array that will contain a variable number of elements based on the changes that occurred on the Flash Communication Server. FCS adds one object to this array for every action it implements on the client. Examples of actions include: clear all of the data, delete a single element, change the value of an existing element, and so forth.

More Stories By Michael Labriola

Michael Labriola is a founding partner and senior consultant at Digital Primates IT Consulting Group. Digital Primates analyzes client business processes and develops custom solutions that extend the latest technology.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Italy News Desk 03/01/06 06:11:57 PM EST

Communication Server (FCS) together provide an amazing toolbox that will undoubtedly provide inspiration to thousands of developers and projects. Unfortunately, without concrete examples and guidelines for good practices on their integration, we spent many hours on tiny issues that, with additional information, could have been easily circumnavigated.