Server-Side Communication ActionScript > Client."commandName"

 

Client."commandName"

Availability

Flash Communication Server MX.

Usage

function onMyClientCommand([p1, ..., pN])
{
	// insert code here
	return val;
}

Parameters

p1, ..., pN Optional parameters passed to the command message handler if the message contains parameters you defined. These parameters are the ActionScript objects passed to the NetConnection.call method.

Returns

Any ActionScript object you define. This object is serialized and sent to the client as a response to the command only if the command message supplied a response handler.

Description

Event handler; invoked when a Flash client or another server calls the NetConnection.call method. A command name parameter is passed to NetConnection.call on the client side, which causes Flash Communication Server to search the Client object instance on the server side for a function that matches the command name parameter. If the parameter is found, the method is invoked and the return value is sent back to the client.

Example

This example creates a method called sum as a property of the Client object newClient on the server side:

newClient.sum = function sum(op1, op2){
	return op1 + op2;
};

The sum method can then be called from NetConnection.call on the Flash client side, as shown in the following example:

nc = new NetConnection();
nc.connect("rtmp://myServer/myApp");
nc.call("sum", new result(), 20, 50);
function result(){
	this.onResult = function (retVal){
		output += "sum is " + retVal;
	};
	this.onStatus = function(errorVal){
		output += errorVal.code + " error occurred";
	};
}

The sum method can also be called on the server side, as shown here:

newClient.sum();

The following example creates two functions that you can call from either a client-side or server-side script:

application.onConnect = function(clientObj) {
	// Add a callable function called "foo"; foo returns the number 8 
	clientObj.foo = function() {return 8;};
	// Add a remote function that is not defined in the onConnect call 
	clientObj.bar = application.barFunction; 
}; 
// The bar function adds the two values it is given 
application.barFunction = function(v1,v2) {
	return (v1 + v2); 
}; 

You can call either of the two functions that were defined in the previous examples (foo and bar) by using the following code in a client-side script:

c = new NetConnection(); 
c.call("foo"); 
c.call("bar", null, 1, 1); 

You can call either of the two functions that were defined in the previous examples (foo and bar) by using the following code in a server-side script:

c = new NetConnection(); 
c.onStatus = function(info) {
	if(info.code == "NetConnection.Connect.Success") {
		c.call("foo"); 
		c.call("bar", null, 2, 2); 
	} 
};