Client class
53
Example
The following 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){
= "sum is " + retVal;
};
this.onStatus = function(errorVal){
= 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);