The Shared Cursor is a great component to demonstrate synchronization using the Flash Communication Server. The concept is this: When data changes, everything connected to the application (with security) must respond to the change. That response is completely up to you. Macromedia has used the object framework to allow you ultimate flexibility in handling data change. There are at least two relationships that you can achieve with synchronization. These forms relate to how you set up the application. A subscriber/publisher relationship can have two forms. A one-way format has no interaction. This is similar to a presentation or a TV-type show with no return channel to receive input from the remote players. Another format for this relationship is similar to a classroom model with a group of students and a teacher facilitating a discussion. The teacher controls time where the participation is a one-way discussion and times where students can interact (multiple-way). Both formats in this relationship usually have two separate Flash movies communicating with a common Server application instance. One Flash movie would have publishing controls built-in such as enabling NetStream publishing for video conferencing, or moderating discussion for a chat window. One very common approach is running a slide presentation, where one Flash movie has the capability to advance slides and publish an audio video stream, while a second (public) Flash movie cannot do anything but watch the presentation. A collaborative relationship features a format where everyone connected can control everything. The previous exercise is a good example of that. Everyone was able to control the WhiteBoard yet maintained sole control of her mouse pointer. This form of communication is perfect for games or collaborative working environments. Socially, it requires the most diplomacy within a group, but that's a whole other chapter. How does Flash Communication Server actually do the synchronization? The server treats each change of the SharedObject as an event. Within Flash MX, the SharedObject uses a new event handler, onSync . The .onSync event handler is triggered when the server announces a change to a SharedObject. In the case of the cursor component, your mouse movement writes the x,y values to the SharedObject. So, every time a mouse is moved, data on the server is changed, and the server announces that change to every Flash player that is connected, including the one that made the change. The .onSync method is invoked by every Flash player when the server sends these announcements. Every connected Flash player receives an Information object containing an array of objects, each with three properties. The array length is equal to the number of data attributes that changed on the server. This architecture is what allows you to support "disconnected" clients , such as mobile devices, where connection times are short and intermittent. For example, when a mobile device connects to the server, it synchronizes itself using the .onSync event looping through the array of objects sent by the server. Each object within the array only carries three parameters. These parameters describe the change to the data. They do not contain the data. It is up to you (using ActionScript) to review the objects and retrieve the data as they describe. The three parameters are code , name , and oldValue . These are important to understand, so let's get into some further detail. CodeThe value of the code parameter is the controller. You base your actions on its value. There are five possible values that code can have:
NameThe name value describes the name of data attribute (or slot) that was changed within the SharedObject. To access the data, ActionScript contacts the server to retrieve the value(s) of each changed data attribute. If the data attribute was an object, you must know the architecture of the object to retrieve the information. If the attribute was an array, you retrieve the entire array and resynchronize it with the local data. The data attribute can be any of the basic ActionScript or JavaScript types including strings, numbers , Boolean, arrays, or objects. It is in the data attribute that you must have a strategy planned for data storage. You do not want data on the server that you do not plan to share. OldValueWhen the code property returns reject or change , this parameter contains the former value of the changed object. This allows you essentially to "override" the change and revert the attribute back to its previous value. Warning The onStatus event is not triggered if an identical value is written to a SharedObject data attribute. If you write a value of Hello World to an attribute that already contains the string "Hello World", onStatus does not run. |