FMS 调用 WEBSERVICE(1)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值