Building Flash applications with Flash Remoting
17
Using Flash Remoting
Like a web browser request for an HTML page, a Flash application that uses Flash Remoting
connects to a remote service and makes a service function call. The service function call is a
client-initiated event. The Flash application makes a request to the remote service; the service
processes the request and returns the results.
It is important to note that the Flash application does not wait for the result; it handles the result
when the service function returns it. The Flash application makes requests from ActionScript to
the server and receives results asynchronously.
This means that the ActionScript code that immediately follows the service request executes
before the result of the call is returned from the server. The following example illustrates this
principle by invoking the
trace()
function immediately after the service request and then again
when the result is returned by the server.
import mx.remoting.Service; // import Service class
import mx.rpc.FaultEvent; // import FaultEvent class
import mx.remoting.PendingCall // import PendingCall clsas
import mx.rpc.ResultEvent // import ResultEvent class
import mx.rpc.RelayResponder // import RelayResponder class
mx.remoting.debug.NetDebug.initialize();
var myService:Service = new Service("http://examples.macromedia.com/
flashservices/gateway",null,"petmarket.api.catalogservice",
null, null); // connect to remote service
var pc:PendingCall = myService.getCatagories({locale:”en_US”}); // call
service method
pc.responder = new RelayResponder(this, "getData_Result", "getData_Fault");
trace( "no response from server yet." );
function getData_Result( re:ResultEvent ):Void { // receive result
trace( "got the response" );
}
function getData_Fault( fe:FaultEvent ):Void { // receive fault
trace( "got the response" );
}
The output from these calls to the
trace()
function would appear in the following order:
no response from server yet
got the response
Notice that the first call to the
trace()
function executes before the result is returned from
the service.
For more information on how to receive and process asynchronous messages from Flash
Remoting, see
“Handling service results and errors” on page 43
.