2007-11-16

Web Services in Flash 9 - AS3: part 2

Re: Carlo Alducente

Thanks for the blessing :) This is my first attempt at publicing code as well as blogging so I'm a bit uncertain about the etiquette, does and don'ts.

Anyway... When trying to create an example of the error handling, I discovered two things:

1. A bug in the call queue. I goofed but now it's corrected.
2. The service allways returned the full response xml. For the moment, I just stripped it so that the content of the Response is returned. The goal is of course to analyse the wsdl and return properly converted data types (strings as strings, ints as ints and complex types as Objects or complex types when the type is registered with a Class alias). This is a project for the future :)

So download the new zip.

...and here is the example:

Service creation:
================================
protected function createService():void {
service = new WebService();
service.addEventListener(LoadEvent.LOAD, onWsdlLoad);
service.addEventListener(FaultEvent.FAULT, onWsdlLoadError);
service.loadWsdl("http://tech.manmachine.se/WebService.asmx?wsdl");
}

protected function onWsdlLoad(event:LoadEvent):void {
trace("wsdl loaded");
}

protected function onWsdlLoadError(event:FaultEvent):void {
trace("wsdl load error: " + event.fault);
}

Service call:
================================
protected function callService():void {
/*
Method 1
Assign IResponder to the AsyncToken
This class implements IResponder
Result is sent to result function and fault is sent to fault function
*/
var token1:AsyncToken = service.Hello("World");
token1.addResponder(this);

/*
Method 2
Listen to events on the AsyncToken
*/
var token2:AsyncToken = service.HelloObject("World");
token2.addEventListener(ResultEvent.RESULT, onResult);
token2.addEventListener(FaultEvent.FAULT, onFault);
}

public function result(data:*):void {
trace("result: " + data);
}

public function fault(info:*):void {
var f:Fault = info as Fault;
trace("fault: " + f);
}

public function onResult(event:ResultEvent):void {
trace("onResult: " + event.result);
}

public function onFault(event:FaultEvent):void {
trace("onFault: " + event.fault);
}

2007-11-15

Web Services in Flash 9 - AS3

While the push from AS2 to AS3 was completely brilliant and Flex2 and Flex3 are absolute heaven for an OOP programmer, Flash CS3 in AS3 is turning out to be a major dissapointment. All the enhancements and improvements in Flex are met by equal and opposite cuts in Flash functionality.

WebServices: Gone
Remoting: Gone
Binding: Gone (well... Binding was introduced in CS3... but only in AS2)

The upside though is that the developer community is strong enough to compensate these blemmishes on the otherwise beutiful face of Flash.

While looking for a WebService package, I stumbled upon Carlo Alducente's brilliant WSDL parser and service wrapper (http://labs.alducente.com/?p=7). The only problem is the error handling... and in order to handle errors nicely, what you really want is an AsyncToken... and a Fault class... and a FaultEvent... And a ResultEvent...etc.

So I rewrote the service package. Although it is packaged as se.manmachine.rpc, all the WSDL parsing and WebService method registering is the work of Carlo. I still haven't recieved his permisson to use his code in this way. Hopefully I will. Kudos Carlo!

The future plans is to extend the package to incorporate RemotingService (RS and WS both extending AbstractService), and using the Proxy class for registering dynamic methods... And then of course adding events for Invoking and headers, wrapping the headers... well... pretty much implementing the whole mx.rpc package for Flash. The future, however, lies in the future.

Download