Tom Muck

Alpha Dog Blues Band
Home page
All articles
All Extensions | Extension News | Extension FAQs | Customer Login
Books authored or co-authored by Tom Muck
Extensions, books, and other products | Customer Login
Your current cart contents
Tom-Muck.com Blog | CMXTraneous Blog | Flash Remoting Blog
About the site

Fr2004pt1pg2

Flash Remoting and Flash MX 2004 Part 1 (cont)

Flash Remoting Examples

The easiest example of Flash Remoting you will find is a simple Hello World example. This one is written for Flash MX, but will work as it stands on Flash MX 2004:

#include "NetServices.as"
var myURL = "http://localhost/flashservices/gateway";
var myServicePath = "com.oreilly.frdg.HelloWorld";

var myResult = new Object();

myResult.onResult = function (data) {
  trace("Data received from Server : " + data);
};

myResult.onStatus = function (info) {
  trace("An error occurred : " + info.description);
};

_global.System.onStatus = myResult.onStatus;
var myServer = NetServices.createGatewayConnection(myURL);
var myService = myServer.getService(myServicePath, myResult);
myService.sayHello();

This can be published as ActionScript 1.0 or ActionScript 2.0. This shows the lazy approach of the ActionScript language: variables are not typed in this example, but the movie will work regardless.

To demonstrate a very simple Remoting app that uses ActionScript 2.0 syntax and the new component architecture, here is the same Hello World service set up to work with a text field named message_txt and button named submit_pb to call the service:

#include "NetServices.as"
var myURL:String = "http://localhost/flashservices/gateway";
var myServicePath:String = "com.oreilly.frdg.HelloWorld";

var myResult:Object = new Object();

myResult.onResult = function (data:Object):Void {
  message_txt.text = "Data received from Server : " + data;
};

myResult.onStatus = function (info:Object):Void {
  message_txt.text = "An error occurred : " + info.description;
};

_global.System.onStatus = myResult.onStatus;

var myServer:Object = NetServices.createGatewayConnection(myURL);
var myService:Object = myServer.getService(myServicePath, myResult);

var simpleListener:Object = new Object();

simpleListener.click = function (evt:Object):Void{
  myService.sayHello();
}

submit_pb.addEventListener("click", simpleListener);

One immediate advantage to typing your variables (as in myURL:String) is that code hints will work in the Flash authoring environment.

Another example is a typical search interface using a data grid. This version works with the new DataGrid that is part of Flash Pro:

#include "NetServices.as"
// Connect to the gateway and create a service object
if (connected == null) {
  connected = true;
  NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
  var my_conn = NetServices.createGatewayConnection();
  var myService = my_conn.getService("com.oreilly.frdg.SearchProducts", this);
}

var simpleListener = new Object();
submit_pb.addEventListener("click", onSubmit);

// Event handler for button
function onSubmit () {
  myService.getSearchResult(search_txt.text);
}
// Responder function for result
function getSearchResult_Result (result_rs) {
  var temp = "";
  temp += "There were " + result_rs.getLength();
  temp += " records returned.";
  results_txt.text = temp;
  // The following line binds the recordset to the grid
  myGrid.dataProvider = result_rs;
}

// Responder function for status
function getSearchResult_Status (error) {
  results_txt.text = "There was an error: " + error.description;
}

Despite the advantages of ActionScript 2.0 syntax, I still advise publishing Flash 6.0 movies for at least the next 12 months for any application that will be utilized on the web. The current infiltration of the Flash 6 player is great, but the Flash 7 player will not become commonplace on the desktop of average users for another year. Flash Remoting has only been around since May of 2002, after all. For company intranets where you know your audience is going to have the latest player, use Flash 7 format. Luckily, Flash offers the option to use ActionScript 2.0 and Flash Player 6 (which works in Flash Player 6.0.79 or higher).

NetConnection Debugger

When you install the Flash Remoting Components for Flash MX 2004, you will notice that the NetConnecton Debugger is conspicuously missing from the Window menu. The debugger is actually installed with the Remoting package, but is located under Window > Other Panels along with the Service Browser panel. Alternatively, you can go to <Flash program directory>\en\First Run\WindowSWF and open the debugger manually before beginning a debugging session. The debugger has a file modification date of 2/26/2003 in the current (as of 9/13/2003 ) implementation of the Flash Remoting Components, so it has obviously not been changed for the release of Flash MX 2004. The panel seems to work flawlessly in Flash MX 2004. Please email me if you find out otherwise.

Of course you can't utilize NetServices in any of your ActionScript 2.0 classes, but Joey Lott has already begun translating the Flash Remoting classes to ActionScript 2.0, at http://www.person13.com/flashremoting/. It's only a matter of time before Flash Remoting will be up to snuff on Flash MX 2004 and Flash Pro.

Conclusion

The initial reaction to Flash MX 2004 and Flash Pro has been mixed. Although the enhancements in the core player, including ActionScript 2.0, are impressive and highly anticipated, the current data binding implementation is a step backward because it is not readily available to Flash Remoting. Macromedia was ahead of the curve with Flash Remoting, but they appear to have taken a step back in the latest version of Flash MX, pushing SOAP ahead of its own AMF technology. Macromedia should use its resources to make AMF the standard for web services used by Flash, rather than fall back on the bulky SOAP standard.

What Macromedia needs to do is get the Remoting adapter onto every server out there, the same way that Flash is on every web browser out there. SOAP is simply too bulky to be of serious use in a client-server environment over the web. Sure SOAP , WSDL, and UDDI are great for small, publicly available services like Amazon, Google, and things like that. But they are really not suited for RIAs that need to pass packets back and forth on 56K lines to web browsers.

Luckily, AMF has taken off in a big way. It has been utilized by several open source projects available freely on the web, as well as several commercial solutions. AMFPHP, for example, (http://www.amfphp.org/) has completely mimicked the standard and created the best solution out there for connecting PHP to Flash. OpenAMF (http://www.openamf.org/) is a J2EE implementation of Flash Remoting that goes beyond what Macromedia's $1000 server component is able to do. FLAP (http://www.simonf.com/flap/) is a Perl implementation. FlashORB is a commercial solution (http://www.flashorb.com/) for J2EE with another in development for ASP. NET.

<< Page 1

Tom Muck, September 13, 2003

Pay me securely with your Visa, MasterCard, Discover, or American Express card through PayPal!
Pay me securely with your Visa, MasterCard, Discover, or American Express card through PayPal!
About | Privacy Policy | Contact | License Agreement | ©2002-2020 Tom Muck | Dreamweaver Extensions