现在的位置: 首页 > 综合 > 正文

Flash Remoting for Flash MX 2004 As2.0 速成

2013年05月08日 ⁄ 综合 ⁄ 共 3394字 ⁄ 字号 评论关闭

P.S. 很抱歉,這份文件裡的專有名詞都是台灣地區的習慣用法,可能大陸地區的朋友看起來會不太習慣,可否請有時間的人修改一下,以造福更多對這有興趣的人。

Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0 網頁

官方網站:請按此

請選擇 flashremoting_comp_as20-win-en.exe 下載。

萬用 ActionScript


import mx.remoting.*; 
import mx.rpc.*; 

// 伺服器回傳資料時,會呼叫此函式 
function myTestFunction_Result (re:ResultEvent):Void { 
trace("回傳資料:" + re.result); 

// 發生處理失敗時,會呼叫此函式 
function myTestFunction_Fault (fault:FaultEvent):Void { 
trace("問題訊息:" + fault.fault.description); 

// 建立一個連線到 127.0.0.1 網站根目錄的 myCFCDir 資料夾中的 myCFC.cfc 
var myService:Service = new Service( 
"http://127.0.0.1/flashservices/gateway", 
null, 
"myCFCDir.myCFC", 
null, 
null); 

// 呼叫 myService 所指向的 CFC 中的 myTestFunctino 函式,並傳入 "123" 做為參數 
var myPendingCall:PendingCall = myService.myTestFunction("123"); 

// 當這個呼叫有回傳訊息時,使用一個 RelayResponder 物件來對它做反應,這個物件在反應時會叫用我們最前面做的 myTestFunction_Result 或 myTestFunction_Fault 函式 
myPendingCall.responder = new RelayResponder(this,"myTestFunction_Result","myTestFunction_Fault"); 

萬用 CFC

<cfcomponent output="no">
<cffunction access="remote" name="myTestFunction" output="false">
<cfargument name="var_01" required="yes" type="any">
<cfset tmp = var_01 & var_01>
<cfreturn tmp>
</cffunction>
</cfcomponent>  

製作步驟

1.首先當然先確定 ColdFusion MX 6.1 ( 或 BlackStone - ColdFusion MX 7 )、Flash MX 2004 及 Flash Remoting AS2 都安裝好了,且 ColdFusion 服務有在執行中。 

2.在網站根目錄中做一個資料夾叫 myCFCDir,裡面開一個新檔案叫 myCFC.cfc ( 若使用 ColdFusion 內建的網站伺服器,網站根目錄是預設在 ColdFusion 資料夾下的「wwwroot」)。 

3.將 myCFC.cfc 的內容改成上列萬用 CFC 的內容。 

4.用 http://127.0.0.1/myCFCDir/myCFC.cfc 網址查看 CFC 的語法是否正確,ColdFusion 會要求你先輸入密碼,這是為了安全起見。 

5.在 Flash MX 2004 開新檔案,在功能表選「視窗」「其他面版」「內建元件庫」「Remoting」,會多跑出一個元件庫,裡面有兩個 Flash Remoting AS2 的元件,將 RemotingClasses 拖一個到場景上,再將它刪除,如此它就會出現在這個新檔案的元件庫中。 

6.將上列萬用 ActionScript 複製到第一個影格的動作面板中,並針對你的環境將程式碼做適當調整。 

7.執行該 Flash 測試是否有 trace 出「回傳訊息:123123」,如果有就成功了。 

8.要做別的 Flash Remoting 就直接拿上面的萬用 ActionScript 及萬用 CFC 修改就好了。  

Flash 的運作過程 

首先由 Flash 來看,一開始就載入了我們需要的這些類別,然後宣告了兩個函式,分別用於處理 ColdFusion 的傳回值,以及在發生錯誤時做相關的處理 ( trace 出錯誤訊息 )。 

接著建立 Service 物件,稱為 myService,這是一個直接指向 CFC 的物件,在這個範例中它指向 IP 為 127.0.0.1 的伺服器 ( 就是本機 ) 根目錄下 myCFCDir 資料夾中的 myCFC.cfc 這支程式。「/flashservices/gateway」是 ColdFusion 用來處理 Flash Remoting 呼叫的路徑,無論你的 CFC 放在這台主機的哪裡,都不會改變。 

再來執行 myService 指向的 CFC 中的 myTestFunction 函式,並將回傳的 PendingCall 物件稱為 myPendingCall,這個物件是 Flash 在送出對 CFC 的呼叫時就立刻建立的,可以將它想像成呼叫 CFC 的這個動作,而非 CFC 回傳的結果。 

最後我們建立一個 RelayResponder 物件,並把它指給 myPendingCall 的 responder。這是專門用來反應 ColdFusion 回傳值的,反應的方法就是一開頭建立的兩個函數,所以在建立這個物件的同時,將那兩個函數名稱丟給它了。 

當 CFC 丟回資料時, myPendingCall 就會叫用自己的 responder,並決定執行成功還是失敗的函式來處理這些資料。  

其他

Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0 還包含很多其他的功能,也有其他的做法來完成 CFC 呼叫。在裝好 Flash Remoting 之後,這些在 Flash 的說明面板中都有,只是英文的讀起來比較不習慣。

Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0 是我活到現在看過最長的軟體名字 ( 嗯...來日方長,只要 Macromedia 還在,也許有機會看到更長的 )。

請別問我為什麼舊版 Flash Remoting 用得好好的要改用新版,我不知道,我也是想搞清楚這一點才學會的。

Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0 包含下列 Class
Connection
DataGlue
Fault 
FaultEvent
Log 
NetDebug
NetDebugConfig 
NetServices 
PendingCall 
RecordSet 
RelayResponder 
RemotingConnector 
Responder 
ResultEvent 
Service 

Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0 包含下列 Package

mx.data.components 
mx.remoting 
mx.remoting.debug 
mx.rpc 
mx.services

原文網址:
http://www.mmug.com.tw/forum/viewtopic.php?t=3683
http://www.aeu-studio.com/Charles/index.cfm?pid=200412170027

以及hiphen整理的简体版本.
http://www.riacn.com/bbs/showthread.asp?postid=15307

抱歉!评论已关闭.