SHARED OBJECTS

If you remember the theory of Object Oriented Programming you know that some classes can have static properties. When we have many instances of the class, normal properties of each instance have different values, but the value of an static property is the same for every instance.

In FlashComm, we have the class SharedObject.

To create an instance of this class at the server, a client needs to use:

someName = SharedObject.getRemote(somenickName, URI);

URI can be captured using the reference-of-instance of the connection: client_nc.uri.

The SharedObject class has a static property whose name is data that is a little misterious. We don't use the property directly. We define "atributes" for data and assign values to them. Something like (so beeing a reference-of-instance of the class):

so.data.qty = 9 ;
so.data.privilege = "admin";
so.data.userName = someVariable;

The "magic" thing is: we have an event-handler that runs when one of this atributes changes.

myRemoteSharedObject.onSync = function(somearray){
 // some code here
}

The parameter is an special array - we will call it: "array of change" - something like a table, having, each line, 3 fields:

The name is the name of an atribute (we can have more than one) that have changed; the oldValue is obvious and code is the more funny field. It will have different values on each user's instance of the class SharedObject. At the user that changed the atribute it will have the string: success and, in the other users it will have: change.

But remember! The value of the atribute (on data static property) is unique for everybody.

How can we use all this theory?

We will make an exercice where an input field can be changed by any user. And the change will be "broadcasted" to the other users.

Try our demo, we create 2 copies of the Flash movie like having 2 users. When you change the field value at left (or right) it will be broadcasted. You can open, at another machine, a copy of this web page. The fields will change there too!

OUR FLASHCOMM SERVER IS AVAILABLE ONLY A FEW HOURS EACH DAY.TRY. AND SORRY IF IT'S NOT AVAILABLE NOW.

.............

The code, glued to the frame is:

stop();
client_nc=new netConnection();
client_nc.connect("rtmp://url/nameofapp");
client_nc.onStatus = function(info) {
 if(info.code=="NetConnection.Connect.Success"){
  _root.msg.text = "Demo: Shared Object";
 }
 else{
  _root.msg.text = "Demo not available now! Try it later.";
 }
}
//Here begin  the new things 
in1.text = ""; //The input field
// Create a remote shared object.  
so = SharedObject.getRemote("sharedtext", client_nc.uri);
so.connect(client_nc);
//Put the value of the input field at an atribute (textValue) of "data"
in1.onChanged = function(){
 so.data.textValue = in1.text;
 _root.msg.text = "Changed by this user";
} 
//The event-handler: 
so.onSync=function(list){
 //In the parameter - list - we can have more than 
 //one atribute that have changed (it's not our case)
 //and we make a search 	
 for (var i = 0; i < list.length; i++)
  if (list[i].name == "textValue" && list[i].code != "success"){
   in.text = text_so.data.textValue;
  _root.msg.text = "Typed by another user";
  break;
 }
} 

The SharedObjec class will be very used at our course.


PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE