This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Server-to-Client Calls
|
343
// This can be called by any client.
Client.prototype.getServerTime = function ( ) {
var now = new Date( );
return now.toUTCString( );
};
As you can see in this example, we define setMyBandwidth( ) and getServerTime( ) on
Client.prototype, making them available to be called by any client movie. Con-
versely, the onConnect( ) method defines another method, kickUser( ), only for cli-
ents who connected with a
username equal to “admin”. If a non-admin client tries to
make the following call:
nc.call("kickuser", null, 3);
it will fail, because the kickuser( ) method is not defined on that Client instance.
As a rule of thumb, attach methods needed by every Client object to
Client.prototype. Define methods on client instances if you want a
subset of your users to be able to invoke those methods.
Server-to-Client Calls
We’ve seen how to invoke a server-side function from a client. Let’s look at the
reverse scenario in which server-side scripts ask a particular client movie to perform
some operation using Client.call( ).
The server-side syntax is very similar to the client-side NetConnection.call( ) method
discussed earlier. Here is the server-side code:
my_client.call(clientMethodName, resultObject | null, [, p1, ...pn]);
where my_client is the instance name of the Client object you want to invoke the
method on.
The first parameter of call( ) is
clientMethodName, a string (such as “test”, or
“setUserID”) that specifies the name of the method to invoke on the client side.
Almost any valid identifier name can be used as a method name. Avoid strings with
spaces and don’t use reserved words and methods such as
call, close, connect,
isConnected, onStatus, and uri.
The second parameter can be either
null or a result object.
The optional parameters
p1, ...pn are passed as parameters to the method on the
client. All the parameter types discussed earlier under “Other Parameter Types” can
be passed to this call as well.
The server-side Client.call( ) method returns a Boolean (
true if the call was sent,
false if it failed immediately). In order for the call to work, you will need to define a

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.