|
Macromedia Flash Remoting: Client/Server Communications
Designing the user interface layout and behavior is the first step in building an application. But connecting the UI to business logic and data is what brings real value to the application. That's one of the reasons that many traditional application developers might want to take a fresh look at Flash. With MX, Macromedia focused on providing a simple, services-based architecture for connecting back-end services with front-end user interfaces. Macromedia calls this approach Flash Remoting.
Flash Remoting provides a collection of client-side APIs for accessing services provided by an application server or SOAP Web services, for working with recordsets and binding them to user interfaces, and for easily debugging interactions with remote services. Flash Remoting also includes a server component (included free with Macromedia's ColdFusion MX and JRun 4) that works with your application server. Macromedia Flash Remoting includes adaptors for nearly any remote service, including simple scripts created in ColdFusion and ASP.NET, as well as well-formed objects and components such as C# objects, EJBs, plain old Java classes, and ColdFusion Components (see the sidebar "Integrating Flash with the ColdFusion Server" for more on ColdFusion Components). It also supports direct invocation of distributed SOAP Web services. The sample code uses ColdFusion Components, because they provide the simplest and fastest way to create a back-end service.
On the client side, a global function initializes when the application loads, and defines a variable that references the remote server that processes the Remoting requests. The example specifies the remote server location using a relative URL, because the Flash Player automatically assumes that the URL points to the origin server of the executing SWF file. In the following example, a proxy object on the client uses a remote service running in ColdFusion MX.
// Create a connection to the Macromedia Flash Remoting service
MyServer = NetServices.CreateGatewayConnection("/flashservices/gateway/");
// Create a local proxy object called employee using a remote service
Employee = MyServer.GetService("EmployeeDirectory", this);
After defining the remote server, you can write functions that handle user interface events and make calls to remote services. For example, here's a function that handles the click event of the search form push button. The function calls the search function on the remote service.
// Function handler for the search form push button
function SearchEmp (firstName, lastName, Dept) {
Employee.GetEmployees({firstname:firstName,
lastname:lastName, Department:Dept});
}
The preceding function uses a single arguments structure that provides named member variables that are then available to an arguments scope in the server component. You could also just pass in an ordered list of parameters, and the arguments would be received in that order on the server, but the server-side developer would need to reference them by their array index value rather than by variable name. Of particular note is that the 'Employee.GetEmployees' method call looks like a local object method invocation, but Flash Remoting transparently maps that to a server function.
In this example, to actually handle the return from the function invocation you need to write a result handler. The Flash Player operates asynchronously with respect to server communication, so you must always write a call back function.
// call back function for the GetEmployees request
function GetEmployees_result (result) {
DataGlue.BindFormatStrings (empList, result,
"#FirstName# #LastName#", "#EmpID#");
}
With Flash Remoting, you can either define a specific Result object that handles all returns, or you can write a generic function that uses the name of the method call plus _result, which will also resolve automatically. This function uses one of the included databinding APIsDataGlueto bind the result data, which in this case is a database recordset, to the empList listbox component. Simple string formatting commands concatenate the firstname and lastname fields as the display values, and the employeeID field as the data value for the list items in the listbox.
That's it: Two functions that handle the user interface event and server results, and a few APIs that handle remote communications and databinding.
|