Home arrow Flash arrow Page 3 - Nonpersistent Client-Side Remote Shared Objects

Nonpersistent Client-Side Remote Shared Objects

If you haven't yet explored the possibilities of shared objects in applications, you're missing out on one of the more important ways to exploit the capabilities of the Internet. Keep reading to learn how you can use shared objects in your web applications. This article, the first of two parts, is excerpted from chapter four of Learning Flash Media Server 2, an O'Reilly PDF Book. Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
January 08, 2009
  1. · Nonpersistent Client-Side Remote Shared Objects
  2. · Instantiating Remote Shared Objects
  3. · Inspecting a Shared Object
  4. · Minimalist Project for Shared Movie Clip

print this article

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)

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 Objects

Whenever 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:


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.)

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials