Flash Media Server Developer Documentation

Client.call()

clientObject.call(methodName, [resultObj, [p1, ..., pN]])

Executes a method on a client or on another server. The remote method may return data to the resultObj parameter, if provided. Whether the remote agent is a client or another server, the method is called on the remote agent's NetConnection object.

Availability

Flash Communication Server 1

Parameters

methodName A string indicating a method specified in the form "[objectPath/]method". For example, the command "someObj/doSomething" tells the client to invoke the NetConnection.someObj.doSomething() method on the client.

resultObj An Object. This is an optional parameter that is required when the sender expects a return value from the client. If parameters are passed but no return value is desired, pass the value null. The result object can be any object that you define. To be useful, it should have two methods that are invoked when the result arrives: onResult() and onStatus(). The resultObj.onResult() event is triggered if the invocation of the remote method is successful; otherwise, the resultObj.onStatus() event is triggered.

p1, ..., pN Optional parameters that can be of any ActionScript type, including a reference to another ActionScript object. These parameters are passed to the methodName parameter when the method is executed on the Flash client. If you use these optional parameters, you must pass in some value for resultObj; if you do not want a return value, pass null.

Returns

A boolean value of true if a call to methodName was successful on the client; otherwise, false.

Example

The following example shows a client-side script that defines a function called getNumber(), which generates a random number:

nc = new NetConnection();
nc.getNumber = function(){
    return (Math.random());
};
nc.connect("rtmp:/clientCall");

The following server-side script calls Client.call() in the application.onConnect() handler to call the getNumber() method that was defined on the client. The server-side script also defines a function called randHander(), which is used in the Client.call() method as the resultObj parameter.

randHandler = function(){
    this.onResult = function(res){
        trace("Random number: " + res);
    }
    this.onStatus = function(info){
        trace("Failed with code:" + info.code);
    }
};
application.onConnect = function(clientObj){
    trace("Connected");
    application.acceptConnection(clientObj);
    clientObj.call("getNumber", new randHandler());
};

Note: This example does not work with version 2 components. For an example of calling Client.call() when using version 2 components, see application.onConnectAccept().