WebServicesPro
MarketingNewz
SmallBusinessNewz












Consuming A WebService With Flash Media Server 2

By Stefan Richter
Expert Author
Article Date: 2007-04-25

Here's a very simple example on how to invoke and consume a WSDL webservice using serverside Actionscript.

You will need Flash Media Server 2 for this, it will not work in Flash Communication Server.

The Webservice I am consuming in this example simply returns the currency exchange rate between two countries. For each country we must pass a string identifying the country, in this case I use 'UK' and 'USA'.

What follows is pure ASC (serverside Actionscript). No .fla file or clientside code is needed to run this.

Here is the content of my main.asc file (you can just copy and paste this code and save it as 'main.asc'):

// load necessary files (the docs don't tell you this...)
load("webservices/WebServices.asc");

application.onAppStart = function() {
  // set up our webservice URL. This service returns an exchange rate between 2 countries
var wsdlURI = "http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl";

    // create the service rateService = new WebService(wsdlURI);

    // Handle the WSDL loading event. rateService.onLoad = function(wsdl){
      trace("--- onLoad");
      trace(wsdl);
      trace("--- end onLoad");
    }

    // If the WSDL fails to load the onFault event is fired. rateService.onFault = function(fault){
      trace("--- rateService onFault");
      trace( fault.faultstring );
      trace("--- end rateService onFault");
    }

    // This invokes the remote method 'getRate' an asynchronous callback. // We use 'UK' and 'USA' as the 2 countries to query. callback = rateService.getRate("UK", "USA");

    // Handle a successful result. This is the important bit :-) callback.onResult = function(result) {
      trace("--- Result");
      trace( "Result. The Exchange Rate is: " + result);
      trace( "1 Pound Sterling will today buy you " + result + " US Dollar.");
      trace("--- end Result");
    }
    // Handle an error result. callback.onFault = function(fault){
      trace("--- callback onFault");
      trace( fault.faultstring );
      trace("--- end callback onFault");
    }
}


In order to run this code you need to create a new application inside your FMS2's applications directory. I called my folder 'wsdl'. Next you should open the Flash Media Server admin console and load the 'wsdl' application. Then hit reload application. You should see the following traces:

Unloaded application instance wsdl/_definst_
WebServices Loaded successfully.
12/16 10:12:34 [INFO] : Queing call getRate
12/16 10:12:35 [INFO] : Made SOAPCall for operation getRate
--- onLoad
String too long to display
--- end onLoad
12/16 10:12:35 [INFO] : Invoking previously queued call getRate
12/16 10:12:35 [INFO] SOAP: Asynchronously invoking SOAPCall: getRate(UK,USA)
param:country1 UK
param:country2 USA
param:Result undefined
--- Result
Result. The Exchange Rate is: 1.7724
1 Pound Sterling will today buy you 1.7724 US Dollar.
--- end Result


Note that the traces such as

param:country1 UK
param:country2 USA
param:Result undefined


are generated by the 'imported' Actionscript files on line 1. They aren't traces produced by my code. Anyone know why param:Result traces undefined?

The documentation on webservices with FMS2 leaves a bit to be desired. My example was based on the code samples of in the live docs.

Comments

Tag:

Reddit | Furl

About the Author:
Stefan is a certified Flash Developer who has been involved with Flash Media Server since its very early days. From his home office in the UK he has handled a variety of projects, specializing in Flash Video and Rich Internet Applications for clients that include CNET, USA Network and Unilever. Stefan is the author of a series of Adobe Developer Center articles, has spoken at several industry events and contributes a regular column on Flash Video to Streaming Media Magazine. His site www.flashcomguru.com is one of the largest online resources on Flash Video.




WebServicesPro is an iEntry, Inc. ® publication - 1998-2008 All Rights Reserved Privacy Policy and Legal