NetConnection.getService( )

NetConnection.getService( ) MethodFlash 6

creates a proxy to a remote service and optionally handles responses
myNetConnectionObject.getService(serviceName, defaultResponder)

Arguments

serviceName

The name of the remote service whose methods you want to access. This varies according to the server model and the type of service you are accessing, as documented in earlier chapters.

defaultResponder

An optional responder object that handles the responses and errors with defined onResult( ) and onStatus( ) methods. If you don't specify the defaultResponder, you must specify responders in your method calls.

Returns

A NetServiceProxy object.

Description

The getService( ) method is used to create a proxy to a remote service and is one of the most frequently used NetConnection methods. The call to getService( ) returns a NetServiceProxy object, which dispatches responses from the service to the client, as discussed in Chapter 4.

Example

The following code shows the basic syntax of the getService( ) method:

#include "NetServices.as"
onResult = function (myResult) {
  results_txt.text = myResult;
};

onStatus = function (myError) {
  results_txt.text = myError.description;
};
var servicePath = "com.oreilly.frdg.HelloUser";
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway");
var my_conn = NetServices.createGatewayConnection( );
var my_service = my_conn.getService(servicePath, this);
my_service.sayHello(firstname_txt.text);

The first parameter passed to getService( ) must be the properly formed service name, as discussed at length in Chapter 1, Chapter 2, Chapter 5, Chapter 6, Chapter 7, Chapter 8, Chapter 9, and Chapter 10. In this case, the method is called with a second parameter, the keyword this, which acts as a responder object. The keyword this represents the current timeline object, and you can see from the code snippet that there are both onResult( ) and onStatus( ) methods defined in the timeline. You can also pass a custom responder object (one created for this express purpose), as shown throughout the book.

The getService( ) method can also be called with no responder object. In that case, the responder object has to be passed in each remote method call, as in this code:

#include "NetServices.as"
function MyResult ( ) {
  this.onResult = function (myResult) {
    results_txt.text = myResult;
  };
  this.onStatus = function (myError) {
    results_txt.text = myError.description;
  };
}
var servicePath = "com.oreilly.frdg.HelloUser";
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway");
var my_conn = NetServices.createGatewayConnection( );
var my_service = my_conn.getService(servicePath);
my_service.sayHello(new MyResult( ), firstname_txt.text);

In the code, which is almost identical in functionality to the previous example, the getService( ) method call does not include a responder object (the second argument is omitted). For this to work without errors, each method call on the my_service object must include a responder object as the first argument. In this case, the first argument is stripped off by the NetServiceProxy object and used as the responder object.

See Also

NetConnection.call( ), the NetServiceProxy object; "Creating the Service Object" and "Creating Responder Functions" in Chapter 4



    Part III: Advanced Flash Remoting
     
    ASPTreeView.com
     
    Evaluation has БЩПexpired.
    Info...