Nonpersistent Client-Side Remote Shared Objects - Inspecting a Shared Object
(Page 3 of 4 )
To get a better idea of what’s going on in a shared object, you can look at the contents something like you’d look at the contents of an array or other object that has more than a single element. Add a folder to your server-side folders named zbasicSO. (This folder will be used for all of the examples in this chapter.)
In the following example, several different types of data are added to the slots. I’ve used literals as well as variables to illustrate the range of data that can be put into shared objects. Just open up a new Flash document and enter the following code in the Actions panel:
1 //Connect
2 var rtmpNow = "rtmp:/zbasicSO";
3 //var rtmpNow = "rtmp://yourDomain.com/zbasicSO";
4 var nc:NetConnection = new NetConnection();
5 nc.onStatus = function(info) {
6 trace(info.code);
7 };
8 nc.connect(rtmpNow);
9 //Shared object
10 var test_so:SharedObject = SharedObject.getRemote("test", nc.uri, false);
11 var rightNow:Date = new Date();12 //Begin list of slots
13 test_so.data.dayOfMonth = rightNow.getDate()
14 test_so.data.money = "$100";
15 test_so.data.verity = true;
16 test_so.data.bunch = 250;
17 //End list of slots
18 for (var attribute in test_so.data) {
19 trace(attribute+" = "+test_so.data[attribute]);
20 }
When you run the script, your Output window should show something like the following (without the parenthetical comments and, unless you are testing this on the 27th of the month, yourdayOfMonthvalue will differ):
bunch = 250(Number)
verity = true(Boolean)
money = $100(String)
dayOfMonth = 27(Date)
NetConnection.Connect.Success
As you can see, shared objects take on the basic data types in ActionScript, and they can be extracted in the same way other objects and variables can be.
Syncing Shared ObjectsWhenever the value of any shared object property attribute, or “slot,” changes, that change must be broadcast to all of those connected. The event handler for this process isSharedObject.onSync(). Each time any data attribute changes, a change event triggersSharedObject.onSync()and in this way, all of the changed attributes in all of the connections are updated. The general format is the following:
my_so.onSync = function(myArrayList){
//Update variables with so.data property attribute values
variable1= my_so.data.attribute1;
variable2= my_so.data.attribute2;
variable3= my_so.data.attribute3;
}
The process for this change is pretty straightforward. Somewhere in the script a user makes a change to one of the slots in the shared objectdata property. As soon as that property is changed, it triggers theSharedObject.onSync()and when that happens, the code in theSharedObject.onSync()container updates the changes throughout the connections in the application.
In order for the whole process to work correctly, you need to call
SharedObject.connect(NetConnection). So to work with remote shared objects meaningfully, you will need a line that connects the shared object instance to FMS2 using theNetConnectioninstance as a parameter. For example:
billz_so.connect(nc);
makes the necessary connection for the shared object instancebillz_soto send and receive the changes to shared objects using theNetConnectioninstancenc. In all of the sample projects in this chapter, keep an eye out for the
SharedObject.connect(NetConnection) statement, and don’t ever forget to include it in your applications. (When you debug your code, often you will find that the shared object connection has been left out, and your shared objects will not run correctly without it.)
Next: Minimalist Project for Shared Movie Clip >>
More Flash Articles
More By O'Reilly Media