More on Nonpersistent Client-Side Remote Shared Objects - A More Than Minimal Text Chat
(Page 3 of 4 )
Now that you have an idea of how shared objects work, this next application shows how to make a pretty good text chat application. Up to this point we’ve kept it simple as far as shared objects are concerned so that you could see the basics. However, now we need to look at the shared object in a bit more detail.
The Shared Object Array
An important element of the SharedObject class associated with the onSync() event handler is the fact that the shared object is an array. As you’ve seen, you can create several slots for shared objects and these slots contain attributes with variable values. When a change to a shared object occurs and invokes the event handler, an argument can be added to an unnamed function to trap the array of objects describing the changed elements of the remote shared objects. Thus, the line,
some_so.onSync= function(so_ObjArray) {
can capture the information in theso_ObjArray. Each object’s properties includecode,name, andoldValue. Because theso_ObjArrayis an array, you need to ferret out its elements (members). The object’s properties’codecan be set to one of the following:
clear
Either successful connection to nonpersistent
remote shared object or objects of a property have
been deleted
success
Client changed shared object
reject
Unsuccessful change of shared object
change
Some other client changed the shared object
delete
Data attribute deleted
Thenamevalue contains the name of the slot. So, if you have three slots, you will have three name values in the array of objects. However, theoldValue is a bit peculiar because it only returns a value other than undefined if thecodevalue is"change".That only occurs if someone from a different client makes a change to the shared object on the same application.
To see how this works, make a few changes to the code in the minimal text chat application discussed earlier in this chapter. When you test it, you need to have two clients open at the same time. In that way you can set thecodeto"change"and see how the oldValue property is revealed. The following code should be used to replace the code in the original minimal text chat application in this chapter. Since it uses atrace()statement to display the values, run all of your tests from within the Flash test mode.
1 //Label button components
2 send_button.label = "Send";
3 var rtmpGo:String;
4 var text_so:SharedObject;
5 rtmpGo="rtmp:/zbasicSO"
6 //rtmpGo = "rtmp://your.domain.com/zbasicSO";
7 var nc:NetConnection = new NetConnection();
8 nc.connect(rtmpGo);
9 text_so = SharedObject.getRemote("sharedtext", nc.uri, false);
10 text_so.onSync = function(list) {
11 for (var i = 0; i<list.length; i++) {
12 trace(list[i].code)
13 trace(list[i].name)
14 trace(list[i].oldValue)
15 if (text_so.data.textValue != undefined) {
16 _level0.chat_txt.text += text_so.data.textValue;
17 }
18 }
19 };
20 text_so.connect(nc);
21 //Send remote shared object
22 var sender:Object = new Object();
23 sender.click = function() {
24 text_so.data.textValue = message_txt.text+newline;
25 };
26 send_button.addEventListener("click", sender);
You will notice that when you enter text on your “local” client, the Output window shows “undefined” for theoldValue. However, when you enter text on your “remote” chat, you will find that the Output windows shows whatever the previous value was.
Next: A Better Chat Application >>
More Flash Articles
More By O'Reilly Media