168
Appendix A: Using NetServices and Connection Classes
// initialization code specifies gateway, creates connection, calls service
if (inited == null)
{
inited = true;
NetServices.setDefaultGatewayUrl("http://localhost:8300/flashservices/
gateway")
var gatewayConnection:Connection = NetServices.createGatewayConnection();
// specify this object (this) as the responder object
howdyService =
gatewayConnection.getService("remoteservices", this);
}
// function specific result handlers
function helloWorld_Result(result)
{
// display successful result
messageDisplay.text = result;
}
function whatsUp_Result(result)
{
// display successful result
resultDisplay.text = result;
}
// default result handler
function onResult(result)
{
trace("result from onResult is: " + result);
}
// call helloWorld service function
howdyService.helloWorld();
// call other service functions
howdyService.whatsUp();
howdyService.howsItGoin();// uses default result handler
Directing results for a service to specific responder objects
You can also specify a responder object to handle the results of a specific service function. To do
this, specify the responder in the first argument of the service call. For example, the following call
to the
whatsUp()
service method specifies
whatsUp_Result()
as the responder.
howdyService.whatsUp(new whatsUp_Result());
Note:
The underlying ActionScript code removes the first parameter before invoking the service
function.
Flash Remoting directs the results to the
onResult
function of the specified result handler.
In the following example the service function calls specify responder objects. There are three
responder objects:
helloWorld_Result()
for the
helloWorld()
function,
whatsUp_Result()
for the
whatsUp()
, and
general_Result()
for other service functions. Each responder object
has one result handling function,
onResult()
, to handle the result returned by the service.
import mx.remoting.NetServices;
import mx.remoting.Connection;