Before you can call a remote service, you must configure Flash Remoting MX:
Note: The client does not access the remote server when you configure Flash Remoting MX. The client first communicates with the remote server when it makes a first service function call.
The following sections describe these steps.
When you configure a Flash Remoting connection, you identify a remote gateway for Flash to use in remote service requests and create a NetConnection object that enables Flash Remoting MX to connect to the remote gateway.
NetConnection object that uses the specified URL.You have several options for doing these steps, including the following:
NetConnection object.
NetServices.setDefaultGatewayURL method to specify a default gateway, and use the NetServices.createGatewayConnection method without a URL to create the NetConnection object. Flash Remoting MX uses the default gateway for all connections that you create without otherwise specifying a gateway.NetConnection object.You can combine these techniques. If you do so, the gateway URL is determined as follows:
createGatewayConnection method takes precedence over any other URL.
NetServices.setDefaultGatewayURL method.Macromedia recommends using the setDefaultGatewayURL method to specify the URL of the gateway that you use in the Flash MX 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.
To configure a connection to a specific gateway, use the createGatewayConnection method; for example:
var gatewayConnection = NetServices.createGatewayConnection("http://apps.mycompany.com/flashservices/gateway");
The gatewayConnection object returned by this method is the NetConnection object for the connection. You use this object to set security credentials, if required, and to get the Flash Remoting service or services that you will use.
This technique has the disadvantage of a hard-coded URL. To change the gateway URL, you must change the ActionScript and publish and deploy your movie.
The setDefaultGatewayURL method sets a default value for the gateway URL, which the createGatewayConnection method uses if you do not otherwise supply the Flash Remoting service URL. For example:
NetServices.setDefaultGatewayURL("http://apps.mycompany.com/flashservices/gateway")
var gatewayConnection = 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.
Setting a default gateway URL provides you with the greatest flexibility in both development and deployment. It lets you provide a URL that works when you test your movie directly in the Flash development environment. It also lets you override the default value with a server-specific gateway when you deploy your movie.
Note: If you specify localhost as the host in the setDefaultGatewayURL method, and you run your Flash application from outside the Flash development environment or stand-alone Flash player, Flash Remoting MX does not use localhost as the default gateway host. Instead, it replaces localhost and any port specified in the setDefaultGatewayURL method with the host and port specified to run the Flash application. For example, if you specify http://localhost/flashservices/gateway in the setDefaultGatewayURL method and start your Flash application by using the URL http://apps.mycompany.com:8500/flashapps/myapp.swf in a web page or browser, Flash Remoting MX uses http://apps.mycompany.com:8500/flashservices/gateway as the default gateway URL.
You specify the gateway in the web page that loads your SWF file by adding entries in the OBJECT tag that calls the SWF file. You must use different techniques for Internet Explorer and Netscape browsers:
flashvars parameter in an HTML PARAM tag inside the OBJECT tag body.
flashvars attribute to the HTML EMBED tag that specifies the Flash application.
The following HTML example runs the myMovie.swf Flash application and specifies
http://apps.myCompany.com/flashservices/gateway as the URL for the Flash Remoting services gateway. The first highlighted line contains the code for Internet Explorer. The second highlighted line contains the code for Netscape.
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="100%"
HEIGHT="100%"
id="MyMovie">
<PARAM NAME="flashvars" VALUE="gatewayURL=http://apps.myCompany.com/
flashservices/gateway">
<PARAM NAME=movie VALUE="MyMovie.swf">
<PARAM NAME=quality VALUE="high">
<PARAM NAME=bgcolor VALUE="#000099">
<EMBED src="MyMovie.swf"
FLASHVARS="gatewayURL=http://apps.mycompany.com/flashservices/gateway"
quality=high bgcolor="#000099"
WIDTH="100%"
HEIGHT="100%"
NAME="movieName"
TYPE="application/x-shockwave-flash"
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
</EMBED>
</OBJECT>
Note: If you specify the Flash Remoting gateway in the web page, the ActionScript #include NetServices.as directive must be on the main movie timeline, not in a movie clip.
The specific format of the URL that you use to specify the gateway depends on the application server to which you are connecting:
http://webServer[:port]/flashservices/gateway
In this URL, flashservices is the name of the Java application context, and gateway is the servlet mapping. If you do not use the default Flash Remoting deployment configuration, replace flashservices with the application context, and replace gateway with the servlet mapping.
http://webServer[:port]/flashremoting/gateway.aspx
In this URL, flashremoting is the logical directory used for Flash Remoting MX, and gateway.aspx is an intentionally blank file installed with Flash Remoting MX; the aspx suffix identifies this as a .NET request. If you do not use the standard installation configuration for Flash Remoting MX, you might need to change these values to reflect your configuration.
Before you access a service function, you must use the getService method of the gateway connection object to create a service object in the Flash client. The gatewayConnection.getService method takes the following two parameters:
The following example shows the getService method for a weather service:
if (inited == null)
{
inited = true;
NetServices.setDefaultGatewayURL("http://localhost/flashservices/gateway")
gatewayConnection = NetServices.createGatewayConnection();
weatherService =
gatewayConnection.getService("flashExamples.weatherStation", this);
}
In this example, you create the weatherService service object.
By specifying this as the responder object, you specify that the current Flash object is also the responder. It is a common practice to create the service object and define the result-handler callback routines in a single Flash object.
The service name format depends on the type of service you are using. The following table lists how you specify service names for the supported service types:
For example, to create a service object for the Babelfish web service, specify a line similar to the following:
babelfishService = gatewayConnection.getService("http://www.xmethods.net/sd/2001/BabelFishService.wsdl", this);
If you are accessing EJBs directly from ActionScript, the gatewayConnection.getService method returns the Home interface of the EJB. You must then use the Home interface create method to access the EJB. For more information, see "Calling EJBs from Flash".
In some cases, your application server might require you to provide user authentication -a user name and password-before you can use a service.
Flash Remoting MX provides the setCredentials method of the NetConnection object to specify authentication credentials to send to the application server. Flash then sends the credentials in the header of every service function call. This method is currently supported on the following servers:
To use the setCredentials method, call the method after you have created the gateway object and before you call a service method. For a Flash application that is run by a single user during a session, you can call the setCredentials method any time after creating the gateway connection object, as follows:
#include "NetServices.as"
NetServices.setDefaultGatewayURL("http://www.mySite.com/flashservices/gateway");
gatewayConnection = NetServices.createGatewayConnection();
gatewayConnection.setCredentials(username, password)
serviceObject = gatewayConnection.getService("myService", this);
Note: To ensure security, never include specific user names or passwords in ActionScript.
Flash supplies the login credentials with each service request; therefore, your ActionScript should log the user out of the application server and reset the credentials when the user logs out of your Flash application.
cflogout tag.)
The following example shows these steps:
myService.logout();
gatewayConnection.setCredentials("", "");
The technique you use on the application server to authenticate the user and authorize access depends on the application server.
If you use ColdFusion MX, the setCredentials method causes Flash Remoting MX to put the login ID and password in the ColdFusion page's CFLogin scope whenever you request a service. The Application.cfm page for the ColdFusion page or ColdFusion component should process this information as necessary in a cflogin tag. For more information on implementing security in ColdFusion, see "Securing access to ColdFusion from Flash Remoting MX".
Authentication in JRun 4 is only meaningful for EJBs, where you can use the setCredentials method to enable access to secured EJB methods. You must configure your application server for single sign on. The Flash gateway provides the security credentials to the login module. For more information on authentication in JRun 4, see "Using Flash Remoting MX with JRun security".