DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 16.6 Storing Persistent Data on the Server

16.6.1 Problem

You want to store persistent data on the FlashCom server.

16.6.2 Solution

Use a remote shared object to store persistent data on the server and access it from one or more clients. Use the SharedObject.get( ) method in the Server-Side ActionScript to create the shared object, and then connect the client(s) to the shared object using the SharedObject.getRemote( ) and connect( ) methods in the client-side ActionScript. Or, you can create the RSO entirely from the client using the SharedObject.getRemote( ) method.

16.6.3 Discussion

To create persistent remote shared objects, you can use one of two approaches. Either you can create the server-side shared object using the SharedObject.get( ) method in an .asc file and then connect to it from the Flash clients, or you can handle everything from the client. Creating the shared object on the server side is preferable in most scenarios because it allows you to do more things with the data on the server. (The server-side .asc file must exist within the FlashCom application. See Recipe 14.1 and Recipe 14.14.) The get( ) method requires you to specify the name of the shared object as well as a Boolean value indicating whether the shared object should be persistent (true) or not (false). (In this case, you want to create a persistent object. See Recipe 17.5 for information about creating nonpersistent shared objects.) If a shared object with the same name already exists on the server, then it is opened; otherwise, a new shared object is created.

// Create a new persistent shared object named myFirstRSO and 
// assign that reference to a variable named myServer_r_so. 
// This code belongs in an .asc file on the FlashCom server.
myServer_r_so = SharedObject.get("myFirstRSO", true);

Once a shared object has been created on the server, you can connect Flash movie clients to it using the client-side SharedObject.getRemote( ) and SharedObject.connect( ) methods. The getRemote( ) method requires three pieces of information: the name of the shared object (which must match the name of the shared object that was created using the Server-Side ActionScript), the URI for the net connection to the FlashCom server (which can be retrieved using the uri property of the net connection object), and a Boolean indicating whether the shared object is persistent (true) or not (false). The getRemote( ) method returns a SharedObject instance. It is used instead of the new operator to instantiate a new remote shared object.

Additionally, you must also instruct the client-side shared object to connect to the FlashCom server to synchronize with the server-side object by using the connect( ) method. The connect( ) method requires that you specify which connection object to use to make the connection.

// Create a new, client-side, persistent RSO in the Flash movie. This code looks for
// a server-side shared object named myFirstRSO on the server and application
// specified by the URI (returned by the myConnection.uri property).
myClient_r_so = SharedObject.getRemote("myFirstRSO", myConnection.uri, true);
myClient_r_so.connect(myConnection);

When any changes are made to a remote shared object, the server automatically synchronizes the client-side data and invokes the onSync( ) method of the client-side shared object for all clients. Therefore, to catch any synchronization events, you should define an onSync( ) method for the client-side shared object. For example:

myClient_r_so.onSync = function (  ) {
  trace("RSO data has been updated/retrieved.");
};

It is important to understand that there will always be some degree of latency between the creation and connection of the remote shared object and the retrieval of the data from the server-side object. Therefore, you cannot rely on the data being available within the client immediately following the calls to getRemote( ) and connect( ). Consider, for example, that you want to use data from a remote shared object to initialize a list box in a client movie. It might seem natural enough to try something like this:

myClient_r_so = SharedObject.getRemote("myFirstRSO", myConnection.uri, true);
myClient_r_so.connect(myConnection);

// Try to set the data provider of the list box to an array stored in the shared
// object (it doesn't work).
myListBox.setDataProvider(myClient_r_so.data.anArray);

However, this will not work! The problem is that, most likely, the remote data has not yet been loaded into the client. This is why it is important that you use the onSync( ) method to catch any updates to the client-side data (which includes the initial retrieval of the server-side data). Here is the correct way to implement the preceding example:

myClient_r_so = SharedObject.getRemote("myFirstRSO", myConnection.uri, true);
myClient_r_so.connect(myConnection);

// Place the setDataProvider(  ) call inside the onSync(  ) method to ensure that the
// server-side data has been retrieved before trying to use it.
myClient_r_so.onSync = function (  ) {
  _root.myListBox.setDataProvider(this.data.anArray);
};

16.6.4 See Also

See Recipe 16.10 for important information on how to store data in the remote shared object. Also refer to Recipe 17.5.

    [ Team LiB ] Previous Section Next Section