View comments | RSS feed

Client.call()

Availability

Flash Communication Server MX 1.0.

Usage

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

Parameters

methodName 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 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 you define and, in order to be useful, 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 executes on the Flash client. If you use these optional parameters, you must pass in some value for resultObject; 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.

Description

Method; executes a method on the originating Flash client or on another server. The remote method may optionally return data, which is returned as a result to the resultObj parameter, if it is provided. The remote object is typically a Flash client connected to the server, but it can also be another server. Whether the remote agent is a Flash client or another server, the method is called on the remote agent's NetConnection object.

Example

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

nc = new NetConnection();
nc.connect ("rtmp://someserver/someApp/someInst");
nc.random = function(){
    return (Math.random());
};

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

application.onConnect = function(clientObj){
    trace("we are connected");
    application.acceptConnection(clientObj);
    clientObj.call("random", new randHandler());
};
randHandler = function(){
    this.onResult = function(res){
        trace("random num: " + res);
    }
    this.onStatus = function(info){
        trace("failed and got:" + info.code);
    }
};

Comments


No screen name said on Feb 10, 2007 at 2:50 AM :
FMS2 client.Call,
Flex2 ??
phslin said on Apr 2, 2007 at 9:49 AM :
In Flex 2/ActionScript 3:
You have to create a object for NetConnectiont.client.

Ex:
On client:
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
nc=new NetConnection();
nc.client=new Object();
nc.client.clientFunc = function (str:String):String{
return "call back strin: " + str;
}
or, create an external class:
public class NetClient{
public function clientFunc(str:String):String{
return "call back strin: " + str;
}
}
then,
NetConnection.defaultObjectEncoding = flash.net.ObjectEncoding.AMF0;
nc=new NetConnection();
nc.client=new NetClient();


on Server:
application.onConnect = function(clientObj){

application.acceptConnection(clientObj);
clientObj.call("clientFunc", new backFunc(),"message");
};

backFunc = function(){
this.onResult = function(res){
trace("result: " + res);
}
this.onStatus = function(info){
trace("fail:" + info.code);
}
}
Robert Reinhardt said on Jul 18, 2007 at 4:54 PM :
Thanks for posting that code sample, but it doesn't seem to work with the slash syntax on the server-side part--if you need to call a custom object and method, such as:

client.call("myCustomInstance/methodName", param1, etc. );

That worked in AS2, but not in AS3. :(

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/fms/2/docs/00000668.html