Accessibility
 
Home > Products > Flash Remoting > Support > CFMX
Flash Icon Macromedia Flash Remoting Support Center - CFMX
Interacting with web services from Macromedia Flash

You use ActionScript to interact with web services directly from a Macromedia Flash movie. In particular, you use the Flash Remoting NetServices ActionScript class and the ColdFusion SOAP proxy. Building a Macromedia Flash movie that calls a remote web service involves the following steps:

Creating the Macromedia Flash movie interface
Connecting to the web service and the ColdFusion SOAP proxy
Calling the web service function
Handling the web service function results

Before you build your Macromedia Flash movie, you must find the web service that you want to use. A number of web service registries exist, including xmethods.net (http://www.xmethods.net) or the IBM Business Registry (http://www.ibm.com/services/uddi). This article uses the BabelFish web service on xmethods.net to translate simple text phrases.

When you browse web service registries on the web, you'll see the acronym UDDI. UDDI, which stands for Universal Discovery, Description, and Integration, is a standards-based technology to define a way to publish and discover information about web services.

 
Creating the Macromedia Flash movie interface
Before you create the Macromedia Flash movie interface, you must think about what parameters the BabelFish web service requires and what results are returned. The BabelFish service accepts two parameters, the string to be translated and the language to translate. Therefore, you need a text box, which users will use to enter a phrase for translation. Also, you need an input control that lets users select the translation language. For this example, a ComboBox UI component is used.

To build the Macromedia Flash movie interface:

1 In the Macromedia Flash MX authoring environment, create a new Macromedia Flash movie from the Web Basic template. From the menu, select File > New from Template. In the New Document dialog box that appears, select the Web category and the Basic template.
2 Insert an input text box into the movie, and name it translate.
In the finished movie, a user enters the phrase to be translated in this text box. The translated phrase returned by the BabelFish web service is displayed in the same text box.
3 Insert a ComboBox UI component, and name it lang. To populate the component, you enter a comma-delimited list of values in the Label and Data fields in the Parameters panel.
In the finished movie, the lang component lets a user select the translation language. To find the list of translation languages that BabelFish service supports, see the BabelFish web service page on xmethods.net for the proper parameter syntax.
4 Insert a PushButton UI component, and in its Parameters panel, enter go_clicked in the Click Handler field.
In the finished movie, when a user clicks the button, the go_clicked ActionScript function is executed.
5 Save the file as babelfish.fla.
When finished, your Macromedia Flash movie should look similar to the following figure.

 
Connecting to the web service and the ColdFusion SOAP proxy
Using the Flash Remoting NetServices ActionScript class, you connect to the BabelFish web service and the ColdFusion SOAP proxy. You use the Actions panel in the Macromedia Flash MX authoring environment to write ActionScript.

To connect to a web service in Macromedia Flash:

1 In the Macromedia Flash MX authoring environment, to open the Actions panel, select Windows > Actions. Ensure that Frame 1 is selected in the Actions panel.
You must add the connection code to the first frame of a Macromedia Flash movie.
2 Because you used the Web Basic template, much of the connection code is written for you and only needs minor modification. Scroll to the bottom of the Actions panel, and review the following code:
if (inited == null)
{
	// do this code only once
	inited = true;
	
	// set the default gateway URL (this is used only in authoring)
	NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
	
	// connect to the gateway
	gateway_conn = NetServices.createGatewayConnection();
	
	//get a reference to a service
	myService = gateway_conn.getService("my.service", this);
	
}
In the code, the if statement ensures that the connection code is executed only once. The setDefaultGateway function specifies the Flash Remoting service URL in ColdFusion. The flashservices and gateway suffixes of the URL do not map to a directory, but to the virtual address of the Flash Remoting service. The Macromedia Flash movie uses the Flash Remoting service URL to invoke the ColdFusion SOAP proxy.
The createGatewayConnection function creates a NetConnection object, which handles many of the details of connecting to application servers or web services in Macromedia Flash. The getService function, which is a function of the NetConnection ActionScript class, creates a reference to the service. No connection to ColdFusion is made until the service function call.
3 If necessary, change the port number of the Flash Remoting service URL to the port number of the ColdFusion server.
4 Modify the getService function to look like the following example:
myService = gateway_conn.getService("http://www.xmethods.net/sd/2001/
BabelFishService.wsdl", this);
The first parameter of the getService function is the URL to the WSDL file for the BabelFish web service. You can find the WSDL URL for the web service on the BabelFish web page at xmethods.net. The this parameter specifies that the results of the service function is returned to the same timeline.
5 Save the file.
Note: It is a best practice to insert the connection code at the end of the ActionScript.

 
Calling the web service function
To call the web service function in ActionScript, you create an ActionScript function that acts as the click handler for the PushButton component in the movie interface. You place the actual remote service function in the click handler function's body.

Note: The Web Basic template creates a generic service function call and handler. To avoid confusion and errors, ensures that you delete these functions before you create any functions of your own.

Create the click handler referenced in the PushButton UI component, as the following example shows:

function go_clicked ()
{
	myService.BabelFish({translationmode:lang.getSelectedItem().data,
		sourcedata:translate.text});
}

In the code, the go_clicked function executes the BabelFish web service function, which is called through the myService service object. The BabelFish function name and the parameter names directly map to the web service method and parameter names of the BabelFish web service. For more information, see the BabelFish web page on xmethods.net.

The parameters are passed as name and value pairs and separated by a colon. For example, translationmode is the name of the parameter, and the getSelectedItem().data function passes the item selected in lang ComboBox UI component. If you use the name/value parameter syntax, enclose the name and value pairs in curly braces ({ }).

 
Handling the web service function results
To handle the results of the web service function, you create an event handler function with the same name as the service functions with _Result appended to the name. The result handler displays the results in the translate input text box, as the following example shows:

function BabelFish_Result(result)
{
	translate.text = result;
}

In the BabelFish_Result function body, the results of the web service function call, represented by the result variable, is assigned to the text property of the translate text box, which displays in the Macromedia Flash movie.

To Table of Contents Back to Previous document Forward to next document