OOP Nature of Flash Communication Server MX

 < Day Day Up > 

Flash Communication Server MX will prove to be a very familiar development environment to you. The server itself takes an object-oriented approach. As noted earlier, Flash Communication Server MX applications are a class. The SSAS (main.asc) defines the application methods , properties, and events.

When using a Flash Communication Server MX application, the application class is instantiated . Each instance of the application is a unique entity on the server. An instance runs completely separate from other instances of the same application.

Here is some ActionScript that invokes an application instance called myInstance :

 my_nc:NetConnection = new NetConnection(); my_nc.connect("rtmp://notReal.macromedia.com/myApp/myInstance"); 

Client Object

Application instances handle client connection requests . Each connection is an instance of the Client object. The Client object also supports a series of prebuilt methods, properties, and events. The Client object monitors everything about a specific client. When the instance is created, some properties that are recorded include IP address, client agent, read/write permissions, and bandwidth information. As the client starts communicating with the server, additional statistics on communication events are also recorded. Information types include byte counts, message counts, streaming info , and tunneling information.

You can extend the Client object class with additional functionality, properties, or listeners. Client instances are linked with the NetConnection instance declared in the Flash Player. Messages can be sent between the NetConnection instance (on the Flash Player) and the Client instance (on Flash Communication Server MX).

The same connect statement also invokes a Client object instance on the server. It is handled by the application.onConnect event.

Remote SharedObject

The Remote SharedObject supplies Flash Communication Server MX applications with a method to share, store, and synchronize information.

We know that to create a SharedObject in Flash after the NetConnection has been established, you use the following ActionScript code:

 var my_so:SharedObject = SharedObject.getRemote("clientSO", my_nc.uri, true); my_so.connect(my_nc); 

This function will create a persistent SharedObject if one doesn't already exist.

If there were a text input instance on the Stage called my_txt , you could send the value of that text object to the RSO with a simple event like this:

 my_txt.onChanged = function() {       my_so.data["my_txt"] = my_txt.text; } 

This would create a slot called my_txt in the data property of the RSO.

Now every Flash Player connected to that SharedObject would have access to that value. What if you changed the value? The event would rewrite it to the RSO with no problem. However, by using the onSync event of the RSO, you can inform all Flash Players that the slot has been updated. Each Flash Player would then update the text objects on their Stage.

In the following step sequence, you discover the Remote SharedObject synchronization event. As you will discover, onSync is a very powerful feature of Flash Communication Server MX applications. Figure 15.7 shows the completed application running twice.

Figure 15.7. Two stand-alone Flash Players running the application twice. One application can speak to another.



Before you begin this exercise, make sure you have installed the Flash Communication Server MX authoring components for Flash MX Professional 2004. You can download the free installer at www.macromedia.com.

  1. Create the application folder. In Windows Explorer, create a folder in the flashcom\application folder (described earlier in this chapter) called FCS-AS2.

  2. In Flash, create a new Flash document, and save it as RSOTest.fla in the folder you just created.

  3. On the Flash Stage, place one input text box above one dynamic text box. In the Properties panel of the input text box, set the instance name to my_txt and the dynamic text box to your_txt. (Remember that you can access the Properties panel for Flash symbols by selecting the object on the Stage.)

  4. Connect the Flash Player to the Flash Communication Server MX instance of the application FCS-AS2. Open the Actions Panel (F9) and enter the following ActionScript code:

     var my_nc:NetConnection = new NetConnection();  connectionLight_mc.connect(my_nc); my_nc.connect("rtmp://localhost/FCS-AS2/myInstance"); 
  5. Connect the Remote SharedObject by using the following code:

     var my_so:SharedObject.getRemote("clientSO", my_nc.uri, true);  my_so.connect(my_nc); 
  6. Overwrite the onChanged event for the input text object, which is my_text . Store the changed data into the SharedObject.

     my_txt.onChanged = function() {       my_so.data["my_txt"] = my_txt.text; }; 
  7. Overwrite the onSync handler for the SharedObject instance. When this event is called, it receives an information object, which is an Array of Objects describing what the change is. This object does not contain the data that was changed; it contains at least two properties: code and name . The code property has five possible values: success , change , reject , clear , and delete . To keep it simple, we will monitor only for a value of change . The name property will contain the slot that was affected.

     my_so.onSync = function(infoObj) {       for (item in infoObj) {   var code:String = infoObj[item].code;   var name:String = infoObj[item].name;             trace("Code: " +code);             //             switch (code) {                   case "success":                   trace("your change was made successfully");                   break;                   case "change":                   your_txt.text = my_so.data[name];                   break;             default :             }       } }; 
  8. The information object is an Array of Objects so that Flash Communication Server MX can send multiple sync notifications at once. For example, after testing this application, shut down the SWF files and load them again. You will notice the last value of the slot appears in the bottom text box.

  9. For this demonstration, understand that if a Flash Player makes a change, it will receive a code value of success . If the Flash Player did not make the change, it will receive a value of change .

  10. Save your application, and publish your movie.

  11. Open two instances of the file RSOTest.swf and enter text into the input boxes (on top). Notice that when you type, the changes are reflected on the other Flash Player. To further test this example, try opening more then two instances of the RSOTest.swf movie.

 < Day Day Up > 

Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net