Article
Getting Started with Coldfusion MX and Flash Remoting
Once you have created the Result class, you're ready to connect to the server. First, set the path to the ColdFusion MX server with the following code. If your ColdFusion MX server is running on a different port or domain name, just change the URL in this ActionScript accordingly.
NetServices.setDefaultGatewayUrl(http://localhost:8500/flashservices
/gateway);
Next, use the NetConnection object to connect to the ColdFusion MX server:
var gw = NetServices.createGatewayConnection();
Note that the Macromedia Flash movie does not connect to the server at this point – instead, think of the object returned by this code as a connection to the server. Next, obtain a reference to the component that resides on the server you want to access:
var server = gw.getService("com.macromedia.test.HelloWorld",
new Result());
Notice that two parameters are passed to the getService method. The first parameter, com.macromedia.test.HelloWorld is the path to our component -- it defines the component that you’re connecting to (you may need to change this parameter based on the location of your component).
The second parameter, new Result(), creates a new instance of the Result object. Passing the new Result () instance to the method indicates that the method should call the functions in that object (the onResult, and onStatus methods) whenever the server sends data or information.
It may seem odd that we’re passing an instance to the Result class by passing a new Result(). We could have done the following instead:
var r = new Result();
var server = gw.getService("com.macromedia.test.HelloWorld", r);
However, as, in this case, we won’t need to use the instance of the class once the data is loaded, we don't need to save an instance of it within a variable.
The getService() method returns an object that represents the actual component on the server. In this case, it is stored in a variable called server. Now, if you want to call a method in your component, call it through the server variable, as follows:
server.sayHello().
At this point, the code calls the service and component on the server. This is how it works:
- Flash MX calls the ColdFusion MX server, passing the component name to the server.
- Flash Remoting locates the component based on the path specified by Flash, and then calls the function within the component (also specified by Flash).
- The component function returns a string.
- The gateway receives the string, and passes it back to the Flash movie.
- Flash receives the string from the server (through Flash Remoting), and passes it to the
onResult()method for the object specified (in this case, theResultclass).
- The
onResultmethod processes the data.
Running the Example
To run the example, and test the movie, use the following steps.
- Verify that the ColdFusion MX server is running.
- Within Flash MX, test the movie by going to Control > Test Movie. "Hello World" appears in the Flash MX output window, as follows:
image
- Publish your Flash movie.
- View your Flash movie in a browser. The ColdFusion component produces the "Hello World" data in the Flash movie.
Conclusion
Now you know how to create a simple Flash movie that uses Flash MX, ColdFusion MX and Flash Remoting. Sure -- displaying "Hello World" may not seem that impressive or useful, however, remember that "Hello World" is simply a "string" object type. You can easily change your ColdFusion component so that it returns other types of data, such as arrays, structures, or database query results. Furthermore, you could use the following server-side code to populate your arrays, structures, or database queries:
- ColdFusion Components (as in this example)
- ColdFusion pages
- Server-side ActionScript
- Java
- Web Services
These ColdFusion MX features help you leverage Macromedia MX functionality on your Website. These topics are featured in the Application Development Centers for ColdFusion MX, Macromedia Flash MX, and Dreamweaver MX.
Reproduced with permission from Macromedia