上次介紹過 Flash Remoting 在 Macromedia 官方支援了三個平台:Java、ColdFusion、.NET,然而,為什麼要使用官方不支援的 amfphp 來 Demo Flash Remoting 呢? amfphp 在執行 Flash Remoting 時最大特色在於不必安裝額外模組,只需確定一些設定即可使用,是最容易上手的一種平台。

那麼就開始進行第一個 Flash Remoting 的工作吧!

首先,要先準備好事前工作,除了基本的 Flash MX 2004 Professional 和架設好的 php 平台之外,還需要準備最重要的兩樣配備:

1. Flash Remoting MX Components
2. amfphp 1.0 milestone 2


將 Flash Remoting MX Components 安裝好之後,並在網頁根目錄建立 flashservices 檔案夾(amfphp官方建議),將 amfphp 1.0 milestone 2 所有檔案解壓縮到 flashservices 裡,這樣就算完成初步準備了。

若以上動作無誤,在網址列輸入 http://localhost/flashservices/gateway.php 將會被告知檔案下載訊息。
之後,所有與 Flash Remoting 有關的檔案必須放置在以下目錄:http://localhost/flashservices/services/
接著依 amfphp 官方提供範例,開始進行第一個 Flash Remoting Demo。

1. HelloWorld.php
<?php
class HelloWorld
{
//建構函數
function HelloWorld()
{
$this->methodTable = array
(
"say" => array
(
"access" => "remote",
"description" => "Pings back a message"
)
);
}

// remoting 方法
function say($sMessage)
{
return 'You said: ' . $sMessage;
}
}
?>

2. 開啟 Flash MX 2004 Professional,選[視窗] > [其他面板] > [內建元件庫] > [Remoting],並將 Remoting.fla 元件庫中的 RemotingClasses 和 RemotingDebugClasses 拖曳到場景中並刪除(這樣此文件元件庫即可擁有 Remoting 兩個元件),並在場景中加入一個動態文字,變數設定為 word,並在影格上加入以下 ActionScript:
// remoting 相關
import mx.remoting.*;
import mx.rpc.*;
import mx.remoting.debug.NetDebug;

// 在 NetConnection Debugger 開始顯示偵錯資訊
NetDebug.initialize();

// 設定 gateway
var gatewayUrl:String = "http://localhost/flashservices/gateway.php"
var _service:Service = new Service(gatewayUrl, null, 'HelloWorld', null , null);

//傳回 php 所取得的資訊並作處理
var pc:PendingCall = _service.say("Hello world!");
pc.responder = new RelayResponder(this, "handleResult", "handleError");

// 無誤時傳回正確結果
function handleResult(re:ResultEvent)
{
trace('The result is: ' + re.result);
word = 'The result is: ' + re.result
}

// 有誤時傳回錯誤報告
function handleError(fe:FaultEvent)
{
trace('There has been an error');
}

3. 將檔案存檔為 FirstRemoting.fla
4. 開啟[視窗] > [其他面板] > [NetConnection Debugger] 以準備進行網路連線偵錯。
5. 按 Ctrl + Enter 測試影片,若正確無誤下面三個視窗將各顯示以下資訊:


第一個採用 amfphp 的 Flash Remoting Demo 順利完成了!

附註:Macromedia Flash Remoting for Flash MX 2004 ActionScript 2.0包含的 Package 與 Class

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

 Class 
Connection
DataGlue
Fault
FaultEvent
Log
NetDebug
NetDebugConfig
NetServices
PendingCall
RecordSet
RelayResponder
RemotingConnector
Responder
ResultEvent
Service

相關連結:amfphp,Testing your AMFPHP installation
2005/08/29 07:50 2005/08/29 07:50
2005/08/29 07:50 

Trackback Address :: http://blog.raienet.com/trackback/41


[◀ PREV][1]... [370][371][372][373][374][375][376][377][378]... [410][NEXT ▶]