164
Appendix A: Using NetServices and Connection Classes
You can also combine these techniques. If you do so, the gateway URL is determined as follows:
1.
A URL specified in a
createGatewayConnection()
method takes precedence over any
other URL.
2.
A URL specified in the deployed web page takes precedence over a default gateway URL.
3.
If you do not otherwise specify a gateway URL, Flash Remoting uses the gateway specified in a
NetServices.setDefaultGatewayUrl()
method.
Macromedia recommends using the
setDefaultGatewayUrl()
method to specify the URL of
the gateway that you use in the Flash authoring environment. Doing so lets you test your Flash
Remoting application directly in the development environment, without having to change your
code when you deploy your application. When you deploy the SWF file, supply the production
gateway URL in the web page that you use to run the Flash application. This way, you also do not
have to make any changes in your Flash application to deploy the SWF file on different
application servers.
Specifying the gateway in the NetServices createGatewayConnection()
method
You use the
createGatewayConnection()
method to configure a connection to a specific
gateway. For example:
import mx.remoting.Connection;
import mx.remoting.NetServices;
// farther in the application . . .
var gatewayConnection:Connection = NetServices.createGatewayConnection("http:/
/apps.mycompany.com/flashservices/gateway");
The gatewayConnection object returned by this method is the Connection object for the
connection. You can use this object to set security credentials, if required, and to access the Flash
Remoting service or services that you will use.
This technique has the disadvantage of using a hard-coded URL. To change it, you must change
the ActionScript, and publish and deploy your SWF file.
Using the NetServices setDefaultGatewayUrl() method
The
setDefaultGatewayUrl()
method sets a default value for the gateway URL, which the
createGatewayConnection()
method uses if you do not supply the Flash Remoting service
URL. For example:
import mx.remoting.NetServices;
import mx.remoting.Connection;
NetServices.setDefaultGatewayUrl("http://apps.mycompany.com/flashservices/
gateway");
var gatewayConnection:Connection = NetServices.createGatewayConnection();
Because you set a default gateway URL, you do not always need to specify the gateway URL
elsewhere. However, you can override the default value by specifying a gateway URL in the
createGatewayConnection()
method or in the web page that calls your SWF file.