Flash Remoting 教學 3 - 透過flash remoting使用 web service
◆flash使用web service 的三種方式
Web service是近五年來非常火紅的資料交換方式,幾乎各大技術平台都有完整的支援方案,flash自然也不例外。
從早期 5.0時又慢又不穩定的xml object到現在7.0快速又穩定的內建核心,flash支援Web service的苦心大家都看得見。
在flash裏使用web service一般有三種方式:
1、 使用web service connector: 在pro版裏有內建這個連線元件,只要簡單在面板上勾選設並填入wsdl就可以進行連線,並且傳回值也可透過data binding或DataSet等元件做進一步處理再bind到UI元件上。
2、 直接使用 web service classes, 一般進行專案時為了能完全掌控Web service object的運作,通常都會選擇直接操作web service class並且建立個ws object來進行連線,然後手工處理傳回值;這個方法聽起來似乎很累人不過由於彈性大選擇多所以還是有好處的。
3、最後一種方式就是透過flash remoting當媒人,先將Web service request傳給server端的remoting gateway, 然後由server去delegate轉向遠端發出Web service請求。這樣做主要的好處有兩個:
-flash player有所謂的sandbox security限制,如果要連線到不同domain的位址往往會出現錯誤訊息,此時要不得一個個手工設定allowDomain,要不就是使用policy xml,但如果透過server端的remoting gateway當媒人的話就不會有domain相異的問題。
-另一個好處就是remoting gateway會自動將傳回值轉換成flash native object,例如最常見的就是 Array, Associative Array,方便直接使用。
當然使用第三種方法也是有缺點的,最明顯的就是連線時多了一道手續,因此會拖慢一些時間,第二個則是所有的request都會透過server端先處理,一來增加server的loading,二來也消耗server的頻寬,因此是否要採用此方法變成是一個高層次的design decision。
本文的目地是教會大家使用這種技巧,但實務上是否採用就請自行判斷囉!
◆透過 flash remoting 使用web service
要透過remoting gateway使用web service 有幾個地方要特別注意:
-由於所有的web service都是過server去轉介,因此server端一定要俱備web service client的能力。這對使用Java, Php平台的人來說更為重要,因為它不像.NET是天生俱備的。
-php 5(windows版)開始正式內建一個 Soap dll,此dll是用C語言撰寫,因此理論上比之前使用NuSoap或其它Soap Library的方案更為快速穩定,但它需要經過一些設定手續才能正式啟用。
◆php的設定
本文使用的平台為 windows xp + apache 2 + php 5.0.4,一般來說安裝好時是沒有將soal_dll掛進去的,大家可以寫一個簡單的 phpinfo() 去看看系統設定,如果沒有找到下面的文字,就代表soap並未啟動。
啟動soap的方式很簡單,只要在php.ini(一般應該都在php的目錄下,例如我的路徑為 c:\php5\php.ini)裏加入下面兩句即可:
extension=php_soap.dll
extension=php_xsl.dll
第一句是掛入 php_soap.dll這個module, 第二句則是支援xslt的功能,雖然remoting用不到,不過如果將來你需要在php裏處理web service的話就很有用,所以順便掛一下吧。
另外請確定php.ini裏面的 extension_dir = “/ext” 的路徑有指向正確資料夾,不然絕對找不到上面兩個module。設定好後存檔將apache 重開一次,如果沒出現錯誤訊息(例如 php_soap.dll找不到之類的)就代表安裝成功了,當然為了保險起見,可以再執行一次 phpinfo(),這次就應該會看到上圖的文字了。
◆php程式碼
當透過 flash remoting來連接web service時,其實php端是不需要寫任何程式的,只要在專案資料夾內有一個 gateway.php 做媒人即可。
不過這裏我們寫一個小程式來測試php是否真的已俱備當soap client的能力。
-
<?
-
$wsdl_url = "http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl";
-
$client = new SoapClient($wsdl_url);
-
$return = $client->getQuote("MACR");
-
?>
寫好後將它存成 testsoap.php然後透過browser執行一次,不久後就會看到macromedia的股價傳回來,今天是40美元一股,當年第一次寫這範例時約略是18美元左右,可見是值得長期投資的績優股,建議買進 (笑)。
這段程式碼比較值得注意的地方是第3行,我們建立了一個 new SoapClient()並給定遠端wsdl的位址,然後下一行透過 $client->getQuote("MACR")去取回macromedia的股價。
◆flash程式碼
接下來我們準備透過flash 呼叫web service吧。
-
/*
-
@author: jeremy@richtechmedia.com
-
@website: http://ria.richtechmedia.com
-
released under CC 2.5.
-
*/
-
-
import mx.remoting.Service;
-
import mx.rpc.RelayResponder;
-
import mx.rpc.FaultEvent;
-
import mx.rpc.ResultEvent;
-
import mx.remoting.PendingCall;
-
import mx.remoting.RecordSet;
-
import mx.remoting.DataGlue;
-
import mx.utils.Delegate;
-
-
//webservie is a folder under /htdoc/ it serveed as a gateway to delegate remote web service call
-
gatewayUrl= "http://localhost/webservice/gateway.php?";
-
-
//initialize NetDebug
-
mx.remoting.debug.NetDebug.initialize();
-
-
//this is where we assign remote web service WSDL
-
service = new Service(gatewayUrl, null, "http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl", null, null);
-
btnSearch.addEventListener("click", Delegate.create(this, doSearch));
-
-
//call remote method to check for current rate between US and Taiwan dollars
-
function doSearch() {
-
var pc:PendingCall = service.getRate({country1:"taiwan", country2:"us"});
-
pc.responder = new RelayResponder(this, "handleSearch", "handleError");
-
}
-
-
//
-
function handleSearch(re:ResultEvent) {
-
trace(re.result);
-
}
-
-
//
-
function handleError(fault:FaultEvent):Void {
-
trace("fault: " + fault.fault.faultstring);
-
}
請注意第17行設定gatewayURL的語法,在gateway.php後面多了一個問號,其實這個gateway.php只是要負責幫忙轉送method call到遠方,因此任何一資料下有這個檔案都可以。
接者第23行我們將要用的web service填進去,這裏我們是使用xmethods.com提供的一個匯率轉換程式。
最後在第28行我們透過 service.getRate()傳出兩個參數,一個是taiwan, 一個是us,表示我們想知道今天一美元可換多少台幣。特別傳參數時使用的格式,裏面是一個anonymous object,包含兩個properties,分別是 country1, country2,這是xmethods.com當時提供web service所規定的參數格式,因此我們一定要遵守,不然call了之就再也不會有回音了…
◆執行
接者來執行看看。
按下button後可透過NCD看到下列訊息
其中第二行是傳送兩個參數給flash remoting,然後它會幫忙轉送給遠端的method。
最後一行則是結果回來了,答案是今天一美元可以換32元台幣,看來是上cdnow血拼的好時機(當初教的時後匯率是令人心痛的35.xxx)
◆結論
這個教學裏我們並沒有使用太複雜的例子,但大家可以到 xmethods.com試試其它的web service, 裏面有許多服務是會傳回複雜的資料型態,例如array甚至是多維的陣列,此時就可以體會使用flash remoting的好處,因為可以省掉自已操作xml(childNode之類的語法)的麻煩。


Trackback this post | Subscribe to the comments via RSS Feed