This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
14
|
Chapter 1: Introducing the Flash Communication Server
Remote Methods
In a typical application, an individual client may need to ask the server to perform
some action on its behalf. For example, it may need to ask the server for the length of
a recorded stream before playing it. The server may also have to ask the client to do
something. For example, the server may need to have the client use a unique string
provided by the server to name the streams the client will publish. Remote method
calls are a way that the client and server can invoke methods on each other.
An application instance can invoke a method on a client using the Client.call( )
method. A client can invoke a method on an application instance using the NetCon-
nection.call( ) method. For example, a server-side script can invoke a method on an
individual client in order to let the client know its unique ID number:
client.call("setID", null, id);
If a client method named setID( ) is invoked using Client.call( ) from the server, the
method must be defined on the client’s NetConnection object or nothing will hap-
pen. For example, the client-side script might look like this:
nc = new NetConnection( );
nc.setID = function (id) {
myID = id;
};
Conversely, the client can call a method on the server using a NetConnection object:
nc.call("getStreamLength", streamInfoResponder, streamName);
For a result to be returned by the remote method, the second parameter passed to
NetConnection.call( ) must be a object with an onResult( ) method.
FlashCom also supports mechanisms to send a remote method request to multiple
clients at the same time. Clients connected to the same shared object or playing the
same stream can all receive a remote method call at the same time. For example, a
popular way to update a text chat area is to use the send( ) method of a shared object
to send a text message to every client:
chat_so.send("showMessage", "Welcome to the chat.");
In this case, a showMessage( ) method must be defined on the shared object or noth-
ing will happen:
chat_so.showMessage = function (msg) {
chatTextArea.text += msg + '\n';
chatTextArea.vPosition = chatTextArea.maxVPosition;
};
Chapter 9 describes calling and defining remote methods in detail.

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.