恩~學一些新東西~
因為看某論壇說到 AMFPHP更新速度慢了!
裡面核心的人物 去開發 zendAMF了~
而且zend有ADOBE的幫助!
還有速度方面會比AMFPHP快!
所以我就想試看看 zendAMF的部分!
下面是我這次測試的整個的部分和遇上的問題!
先講一下我的系統
OS WIN7
Apache 2.2.8
PHP 5.2.6
Zend AMF 1.11.11 (我本篇測試是使用傳說中的光棍板!)
FLASH Builder 4.5 (是使用官方下載的 4.5 的原生SDK )
基本上~應該相關的就這幾個!
1. 先拿到檔案
zendAMF
http://framework.zend.com/download/amf
Appser(我是灌AppServ 2.5.10)
http://www.appservnetwork.com/?appserv
FB
http://www.adobe.com/downloads/
這邊我測過SDK 4.5.1 官網直接下載板 OK
還有置換過 AIR為 3.1版本的 4.5.1 都能順利執行本次測試!
不過為了通用起見~就用原本 4.5.0的SDK作講解
2. 安裝 zendAMF
步驟很簡單~解壓縮到你的 wwwRoot下~
以我的來說是在
C:\AppServ\www
以用光棍板來說解壓後是叫
ZendAMF-1.11.11
在
C:\AppServ\www\ZendAMF-1.11.11
我是覺得他的名稱太長所以我改成 zendAmf
然後在你的 zendAmf下建立一個PHP
getway.php (是參考
http://blog.corausir.org/programing/ausir-823 寫出來的 )
<?php
ini_set("display_errors", "On");
define ('P_S', PATH_SEPARATOR); //設定P_S是分隔符號;
set_include_path('.' .P_S .'library' .P_S . get_include_path()); // 設定include路徑
require_once 'library/Zend/Amf/Server.php'; //引入 AMF SERVER 類別
$server = new Zend_Amf_Server(); //建立 zendAmf的實體!
$server->addDirectory(dirname(__FILE__).'/services/'); //將整個資料夾都納入 SERVER 內
$result = $server->handle();
echo $result;
?>
先測試一下getway.php 是不是只輸出
Zend Amf Endpoint
如果是的話~那就是正確安裝了~
======如果有錯請看==========
如果不是就 看錯誤除錯吧~
話說我一開始getway.php 是沒有 define 和 set_include_path會報一堆錯~
然後我是傻傻的一個一個PHP去除錯~
基本上都是 require_once 找不到檔案!
後來我除到手痠~就COPY了一份 zend的到zendAmf的目錄下!
可以正常執行 但這個方法太笨!
後來查一查~發現這個寫法~蠻不錯的 建議大家一定要用 set_include_path
其實整個zendAmf只有用到 library下的東西~
其他的我稍微看過了一下是測試和DOC基本上還是有幫助的!
所以我建議也是裝了吧!
3. 當然是 helloworld
先來 AS 版的!
先用 fb建立一個 as project (因為這個比簡單~設定的也少! 而且這個OK 基本上FLEX版的才會OK)
package
{
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
public class testZendAmfAS extends Sprite
{
public function testZendAmfAS()
{
var nc:NetConnection = new NetConnection();
NetConnection.defaultObjectEncoding = ObjectEncoding.AMF3; //設定傳輸的方式!
nc.connect('http://localhost/zendAmf/gateway.php'); //你的getway.php的路徑!
var res:Responder = new Responder(onResultFun, onError);
nc.call("helloWorld.sayHelloWorld",res);
// 使用 Remote 方式連結 helloWorld 呼叫下面的 sayHelloWorld函數 傳回給 res
}
private function onResultFun(e:Object):void
{
trace(e + " Result!");
}
private function onError(err:Object):void
{
for (var i:String in err) {
trace(err[i] + ' ' + i + ' Error');
}
}
}
}
然後是 PHP的部分~
剛剛還記得
$server->addDirectory(dirname(__FILE__).'/services/');
把
C:\AppServ\www\zendAmf\services
下的PHP都會被引用近ZENDAMF裡可以供呼叫!
這樣就像是我用慣的AMFPHP的方式!
基本上我拿了一些簡單的AMFPHP我寫的services 是都可以直接套用
不過拿一個最近案子的services 因為比較複雜~雖然沒錯! 但是整個不能運行!
現在就來一個簡單的 PHP
<?php
class helloWorld {
public function sayHelloWorld(){
return "zendAmf is Running! Say Hello World!";
}
}
?>
這個PHP是放在
C:\AppServ\www\zendAmf\services 下喔!
雖然有看到說可以放子目錄的方式!
但是我試過都不成功!
有心的人可以自己試看看!
基本上~
應該會看到
zendAmf is Running! Say Hello World!
的output在輸出面板!
到此為止你的 FLASH 和 zendAmf 已經正常得運行和聯動了!
接下來來一些比較高端的部分~
使用 flex的MXML去聯結 zendAmf的部分!
PHP的部分是一樣的
所以我就只講 FLEX的部分
一開始建一個 flex project
記得要按 next去設
web root
和 root url
以我的來說
web root : C:\AppServ\www
root url : http://localhost/
然後是寫 services-config.xml 放在src下喔!
這個很重要!
要注意的點有幾個!
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="sabreamf-flashremoting-service" class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="zendAmf">
<channels>
<channel ref="my-amfphp" />
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost/zendAmf/gateway.php" class="flex.messaging.endpoints.AMFEndpoint" ></endpoint>
</channel-definition>
</channels>
</services-config>
endpoint uri="http://localhost/zendAmf/gateway.php"
這個很重要! 不要設錯了!
destination id="zendAmf" 這個也很重要~後面設定MXML會用到~
剩下的請自己去查巴!
對了~記得對你的 project按右鍵!
properties->flex compiler
記的多一行
-services "services-config.xml"
這樣才算設定完成!
然後是重頭戲! MXML的部分!
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init();"
minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.InvokeEvent;
import mx.rpc.events.ResultEvent;
private function init():void{
amf.addEventListener(FaultEvent.FAULT, onFaultFun); //如果傳回錯誤的監聽
amf.addEventListener(InvokeEvent.INVOKE, onInvokeFun);//如果傳出指令的監聽 理論上可以不設! 不過要了解所有傳出的動作可以從這裡做!
amf.addEventListener(ResultEvent.RESULT, onResultFun);//除理傳回值的部分! 雖然這三個都可以透過 MXML去設!
amf.sayHelloWorld(); //這個就是呼叫 HelloWorld函數的部分!
}
private function onFaultFun(e:FaultEvent):void
{
trace(e.toString() + " 1!!!");
}
//
private function onInvokeFun(e:InvokeEvent):void
{
trace(e.toString());
//trace(e.message.toString() + " ");
}
//
private function onResultFun(e:ResultEvent):void
{
trace(e.result + "End Result.");
// trace(e.headers + " headers");
// trace(e.message + " message");
// trace(e.messageId + " messages !");
// trace(e.statusCode + " statusCode");
// trace(e.target + " tager" );
// trace(e.token.responders + " token.responders ");
// trace(e.type);
}
]]>
</fx:Script>
<fx:Declarations>
<s:RemoteObject id="amf" destination="zendAmf" source="helloWorld" />
</fx:Declarations>
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
</s:layout>
</s:Application>
<s:RemoteObject id="amf" destination="zendAmf" source="helloWorld" />
是設定你連結的方式!
id="amf" 是指你這個連線的名稱 後面用來呼叫函數 或是 監聽用的
destination="zendAmf" 就是你設定的 services-config.xml裡的
destination 的設定~
記的填一樣的~不然會報錯!
source 就是你servers的PHP的類別名稱
好了自己測試巴!
基本上你會先看到 InvokeEvent 代標有傳出呼叫了!
然後會有 onResultFun 近來!
到此兩種方式都介紹了!
個人是喜歡用 AS 的方式~
因為統一! 而且FLEX也可以用喔!
4.進接的應用VO的使用!
恩~依照 http://blog.corausir.org/programing/ausir-855的介紹!
基本上按照他的作都OK
只有一步!在新的版本上~會報錯!
這個錯我找了很久~很久~
後來發現!不用設定
$server->setClassMap
和
$server->setClass
就可以直接對映!
這應該是新版本的威能!
所以我進行了一些測試!
基本上~只要在VO端加上
[RemoteClass(alias="PHP的類別")]
就可以直接 強制轉型!
var cv2:VO2 = e.result as VO2;
假設試不能轉的類型
會變成 null
假設兩個 AS的VO都設同一個 php class
只有最後設(讀到的)
可以強制轉型!
前面的就算有寫 RemoteClass
也會變能 null
好了整個zendAmf的部分 測試完成!
謝謝觀看!
順便記錄一下!
基本上如個這個有下一篇應該試作和 amfphp 的一些測試評比
說一下心得
不過就一些簡單的傳輸來說~
我是看不出差別!
說真的這個比 amfphp 難設定!
目前我也沒發現簡易的後台~
像AMFPHP有一個試調和測試的介面!
如果有的話高手介紹一下!~
不然只好自己生了~~
不過兩個在轉換上應該問題不大!
目前只有一個超複雜破 三千行又include一堆東東的server跑不起來~
其他簡單的都是 OK的!