View comments | RSS feed

Building a Hello World application with Flash Remoting

In this section, you build a simple Flash application that uses Flash Remoting MX to connect to four different remote services, including a ColdFusion page, a JavaBean, an ASPX page, and a web service. You will see that Flash applications require minimal changes to call different remote services.

To build the Hello World application, see the following sections:

Building the remote service

Flash Remoting MX supports Java, ASP.NET, and ColdFusion-based remote services. For a simple Hello World application, the following table lists the application server code by platform and where to save the file to make it available to Flash Remoting MX:

Application Server

Application server code

File location
ColdFusion MX
<cfset flash.result = "Hello from ColdFusion MX!">
Save the ColdFusion page as helloWorld.cfm in a folder named remoteservices under your webroot.
JRun 4
package com.remoteservices;

import java.io.Serializable;

public class FlashJavaBean
  implements Serializable {

  private String message;

  public FlashJavaBean() {
    message = "Hello from Java!";
  }
  public String helloWorldJava() {
    return message;
  }

}
Save the compiled JavaBean class file to classes/com/remoteservices folder under the SERVER-INF directory.
ASP.NET
<%@ Register TagPrefix="Macromedia" Namespace="FlashGateway" Assembly="flashgateway" %>
<%@ Page language="c#" debug="true" %>
<Macromedia:Flash ID="Flash" runat="server">
  Hello from .NET!
</Macromedia:Flash>
Save the ASPX page as helloWorldNET.aspx in the flashremoting directory under your webroot.

For more information about application server-specific documentation, see the following chapters:

Calling the remote service from ActionScript

To build a Flash application that uses Flash Remoting MX, you write ActionScript in the Flash MX authoring environment that connects to the remote service and calls a service function. For more information on building Flash applications that use Flash Remoting MX, see Chapter 2, "Using Flash Remoting Components in ActionScript".

To build the Flash application that calls remote services using Flash Remoting:

  1. In the Flash MX authoring environment, create a new Flash file, and save it as serviceTest.fla.
  2. In the document window, insert a text field and a PushButton UI component.
  3. Select the text field with your mouse. In the Properties Inspector, name the text field messageDisplay, and select Dynamic Text in the menu.
  4. Select the PushButton UI component with your mouse. In the Properties Inspector, enter button_Clicked in the Click Handler parameter, and enter "Anyone there?" in the Label parameter.
  5. Open the Actions Panel, and select Actions for Frame 1 of Layer 1 in the menu. Insert the following ActionScript code:
    //imports the NetServices ActionScript file
    #include "NetServices.as"
    //if statement creates the connection to the remote service and creates a service object
    if (inited == null)
    {
      inited = true;
      NetServices.setDefaultGatewayURL("gatewayURL");
      serviceConnection = NetServices.createGatewayConnection();
      serviceObject = serviceConnection.getService("serviceName", this);
    }
    //function executes when the user clicks the button
    function button_Clicked()
    {
      //service function call to the remote service
      serviceObject.serviceFunctionName();
    }
    //if the service function is successful, the _Result function of the same name executes
    function serviceFunctionName_Result(result)
    {
      messageDisplay.text = result;
    }
    //if the service function is unsucessful, the _Status function of the same name executes
    function serviceFunctionName_Status(result)
    {
      messageDisplay.text = error.description;
    }
    

    In the code, the ActionScript creates a reference to the remote service using the NetServices functions setDefaultGateway, createGatewayConnection, and getService. You specify the following variables:

  6. Save the file.

Using the previous ActionScript as a template, the following table lists the values for service name and service function variables:

Remote service
Service name
Service function name
ColdFusion MX
remoteservices
helloWorld
JRun 4
com.remoteservices
helloWorldJava
ASP.NET
remoteservices
helloWorldNET
Web service
http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl
getQuote

For more information about application server-specific service name and service function names, see the following chapters:

Comments


J___ said on Jan 10, 2004 at 7:44 PM :
Wouldn't the service name for .Net be flashremoting and not remoteservices?
halL said on Jan 12, 2004 at 7:31 AM :
You are correct.

See:
http://livedocs.macromedia.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRNET3.htm#1169986

For calling ASPX pages, the service name matches the directory structure from the webroot, and the documentation refers to an ASPX page called "helloWorldNET.aspx" under the flashremoting directory under your webroot.

In .NET, you can create web applications very easily - they don't have to exist in a directory called flashremoting - that's just the default for the sample apps.
melo-mel said on Mar 26, 2004 at 10:46 AM :
The line that reads NetServices.setDefaultGatewayURL("gatewayURL");
should read NetServices.setDefaultGatewayUrl("gatewayURL");

Flash MX 2004 does not compile as the compiler is now case sensitive.
Dawnthea said on Aug 4, 2004 at 12:09 PM :
Is there some other documentation that relates to Flash MX 2004? When I try to do these steps, I get the following actionscript errors:

**Error** Scene=Scene 1, layer=actions, frame=1:Line 2: Error opening include file NetServices.as: File not found.
#include "NetServices.as
No screen name said on Aug 18, 2004 at 10:42 AM :
#include "NetServices.as has been depracated in MX 2004.
Try this

import mx.remoting.NetServices;
or
mx.remoting.Service.Service
phi463 said on Oct 31, 2004 at 5:27 PM :
I was getting the same error, File not found. #include "NetServices.as".

I tried
import mx.remoting.NetServices;
or
mx.remoting.Service.Service
either of these got rid of the above error but I am also getting the following:
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 28: There is no property with the name 'onStatus'.
System.onStatus = myResult.onStatus;

I got the code from the "helloworld" example. I am just trying to learn Flash Remoting and I can't seem to get it to work straight from Flash.
Any help would be greatly appreciated!
No screen name said on Nov 13, 2004 at 9:52 PM :
I have been learning amfphp and spent hours on this problem the i added this line instead of #netservices.as
import org.amfphp.remoting.NetServices;
works perfect
hope this helps
No screen name said on Nov 17, 2004 at 2:27 PM :
The documentation doesn't mention it, but the argument to setDefaultGatewayURL must be a full URL, like http://myhost.com/flash/gateway. Just passing the URI, like "/flash/gateway", won't work. Also, using the Windows net service name, like "http://mywin2kserver/flash/gateway" won't work, even if your browser can resolve it.
No screen name said on Nov 17, 2004 at 4:53 PM :
One good way to set the gateway URL for a swf that will be deployed to multiple environments (e.g., testing, integration, production) is to call NetServices.getHostUrl(). For example, :

var url = NetServices.getHostUrl();
NetServices.setDefaultGatewayUrl(url + "/flashremoting/gateway");
NetServices.createGatewayConnection();

That way it'll always connect back to the server in which it's running.
No screen name said on Dec 3, 2004 at 10:46 AM :
I can not get this to work. Do I need the include files to be in the same directory???
$@!#7 said on Jan 25, 2005 at 9:11 AM :
To those who just added the following AS

#include "NetServices.as"
#include "NetDebug.as"

and experience this type of errors:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 1: Error opening include file NetServices.as: File not found.

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 2: Error opening include file NetDebug.as: File not found.

I kinda found an answer. I'm working with Flash 2004pro and upgraded it to 7.2 and installed the
flashremoting_comp_as20-win-en.exe
for flash remoting, but keep on getting these errors. But after I installed the FlashRemotingComponents-win-en.exe
the errors were gone and I'm getting good results in my NetConnection Debugger.

Hope this helps someone.
lakhanpalv said on Feb 15, 2005 at 10:01 PM :
When i am using "http://localhost/webdirectory/gateway.aspx" in the url passed to setDefaultGatewayUrl() it works. But when i use IP address instead of "localhost" it doesn't works. What can be the possible reason.?
alex786 said on Mar 8, 2005 at 5:27 AM :
$@!#7 i had the exact same problem as you.
@include "NetDebug" just would not work even though i have installed Flash 2004pro and upgraded it to 7.2 and installed the
flashremoting_comp_as20-win-en.exe

I followed what you did, and mine is also now working

Thankyou
extdw_doc said on Mar 8, 2005 at 11:44 AM :
Some of these comments indicate that people are using Flash Remoting MX documentation with the "Flash Remoting for Flash MX 2004 ActionScript 2.0" product.

There are two different versions of the Flash Remoting documentation at:

http://livedocs.macromedia.com/flashremoting/

If you're using Flash Remoting for Flash MX 2004 ActionScript 2.0,
click the LiveDocs link for that product, then select either "Using Flash Remoting for Flash MX 2004 ActionScript 2.0" or "Flash Remoting for Flash MX 2004 ActionScript 2.0 API Reference", depending on what you're looking for.

If you're still using Flash Remoting MX (older product), choose the Flash Remoting MX documentation.
hoyerjd said on Aug 11, 2005 at 7:01 PM :
$@!#7 I had the exact same problem on one machine when I had installed the ActionScript 2.0 components only but the same file did not cause an error on a different machine which I had installed the ActionScript 1.0 components on first and then later the ActionScript 2.0 components. I added the ActionScript 1.0 components to the other machine and it worked fine. THANK YOU, this had been very frustrating.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/intro4.htm