Section 8.10. Broadcasting Remote Method Callswith send( )

8.10. Broadcasting Remote Method Callswith send( )

When data changes in one copy of a shared object, that data is copied to every other copy of the shared object, after which every shared object's onSync( ) method is called. Shared objects provide another way to broadcast information to every copy of a shared object without storing data or calling the onSync( ) method. Custom methods of the shared object can be defined so that they can be executed and passed data. For example, a custom showMessage( ) method can be defined that will show text passed to it within a text field. One copy of a shared object can then send some text to every copy of the shared object and ask that the text be processed by the method named showMessage( ) . Asking remote shared objects to execute a method is one mechanism of remote method invocation (RMI); see Chapter 9 for more information on RMI.

A request to invoke a method will succeed only on the shared objects that define a method with that name . Requests to invoke a method are made using SharedObject.send( ) . A common use for send( ) is to broadcast the text messages that show up in a chat component. As an oversimplified example, if this statement is executed:

 messages_so.send("showMessage", "Hi Everyone"); 

then the showMessage( ) method of every copy of the messages_so shared object is called and passed the text "Hi Everyone". Here is one implementation of showMessage( ) that just displays the text in a text field and scrolls the text field down as far as possible:

 messages_so.showMessage = function (msg) {   chat_txt.text += msg + "\n";   chat_txt.scroll = chat_txt.maxscroll; }; 

Note that showMessage( ) is defined as a property of the shared object, not attached to the shared object's data property.

8.10.1. Text Chat Example

The previous two snippets of code do not show how the shared object is created and managed in a real application. A common way to create a text chat is to create a Chat object or component that contains a shared object. When a message needs to be sent to the other movies participating in the chat, the user will click a button or press the Enter key so that a method of the Chat object is called. It will retrieve text from an input field and then send it using the shared object's send( ) method by invoking a custom method named showMessage( ) :

 Chat.prototype.sendMessage = function (  ) {   var msg = trim(this.send_txt.text);   if (msg == "") {  // Don't send empty messages     return;   }   this.send_txt.text = ""; // Clear the text field   this.messages_so.send("showMessage", this.userName + ": " + msg); }; 

The Chat object is also responsible for getting the shared object and defining its showMessage( ) method:

 Chat.prototype.init = function (nc, userName) {   this.messages_so = SharedObject.getRemote("public/textchat/messages", nc.uri);   this.messages_so.owner = this;   this.messages_so.ready = false;   this.messages_so.onSync = function (list) {     if (!this.ready) {       this.ready = true;       this.owner.onMessagesReady(this);     }   };   this.messages_so.showMessage = function (msg) {     this.owner.showMessage(msg);   };   this.messages_so.connect(nc); }; Chat.prototype.showMessage = function (msg) {   this.chat_txt.text += msg + "\n";   this.chat_txt.scroll = this.chat_txt.maxscroll; }; Chat.prototype.onMessagesReady = function (so) {   this.setEnabled(true); }; 

This example should look a little like the previous shared ball example. The Chat object contains a shared object that no other class uses, just as the SharedBall class contained a shared object. The shared object is stored as a property of the Chat object:

 this.messages_so = SharedObject.getRemote("public/textchat/messages", nc.uri); 

Also, as in the shared ball example, the shared object is given a private property named owner that holds a reference back to the Chat object:

 this.messages_so.owner = this; 

When a showMessage broadcast arrives, the shared object's showMessage( ) method uses its owner property to pass the text message on to the Chat object:

 this.owner.showMessage(msg); 

In the Chat class example, the shared object acts as a conduit for broadcasting text messages between chat objects. It does not store any data.

The accumulated text of the chat session is not stored in the shared object. Doing so might require very large messages to be sent, consuming too much bandwidth. Instead, the newly entered text is passed as a parameter to the showMessage( ) handler. Each client is responsible for maintaining and updating its own copy of the text; latecomers do not receive text that was sent before they joined the chat.


8.10.2. Differences Between Shared Objects and NetStream.send( )

NetStream and Stream objects can publish send( ) method requests very much like shared objects can broadcast method requests. (See "Sending a Request" in Chapter 5.) However, there are some important differences between how shared objects and streams handle send( ) requests. When a shared object sends a remote method invocation request, the request is sent to the server and then to every copy of the shared object, including the shared object that originated the request. In the example of the text chat, that means the request to invoke a method is also sent back to the originator of the message. In the previous example of the text chat object, the text sent by a user will not show up on the screen until it is received back from the server. The NetStream.send( ) method works differently. There is only one publisher of each stream, and when the publisher sends a message, only the subscribers to the stream receive the method request. NetStream subscribers cannot invoke send( ) to send remote method requests, and the publisher does not receive back its own send requests. The other important difference is that recorded streams will store any method requests made on them in the .flv file of the stream so that the recorded methods will be executed when the stream is played later. Shared objects have no internal mechanism to record method calls (that is, remote method calls invoked via a shared object are not automatically stored or retrievable).



Programming Flash Communication Server
Programming Flash Communication Server
ISBN: 0596005040
EAN: 2147483647
Year: 2003
Pages: 203

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