Macromedia flash media server 2-developing media applications User Manual

Page of 114
52
Using Media Classes
The SharedObject class
Shared objects can be used for a wide number of purposes, and can be designed and used in a 
number of ways. This section provides some items to consider as you begin using shared 
objects in your applications. 
About synchronizing shared objects
Before attempting to work with a remote shared object, you should first check that the 
SharedObject.connect()
 method returned a value of 
true
, indicating a successful 
connection, and then wait until you receive a result from the function you have assigned to 
the 
SharedObject.onSync
 handler. If you fail to do so, any changes you make to the object 
locally—before 
SharedObject.onSync
 is invoked—may be lost.
The 
SharedObject.onSync
 handler is invoked when any of the 
SharedObject.data
 
properties of the remote shared object are changed. The handler is also invoked the first time 
a client uses the 
SharedObject.getRemote()
 method to connect to a remote shared object 
that is persistent locally or on the server. In the latter case, all the object properties are set to 
empty strings, and 
SharedObject.onSync
 is invoked with the value of the 
code
 property set 
to 
"clear"
. Then 
SharedObject.onSync
 is invoked again, this time with the value of 
code
 
set to 
"change"
, and the properties of the client’s instance of the remote shared object will be 
set to match those on the server. 
If you are having problems understanding how your shared object is behaving, it helps to 
place some debug code in your 
SharedObject.onSync
 handler, as in the following example:
my_so.onSync = function(list) {
for (var k in list) {
trace("name = " + list[k].name + ", event = " + list[k].code);
}
// do whatever else you want to do here
}
Using shared object slots effectively
The decision to encapsulate shared object data in a single slot (
SharedObject.data
 property) 
or to spread it among multiple slots depends partially on how your application posts changes 
to the object. For example, if your application needs to send an entire array of strings to all of 
the connected clients at specific intervals, you can store the entire array in a single slot.