2010年6月1日 星期二

AMFPHP Flex Remote

§Flex AMFPHP Remote測試
承下兩篇安裝
  1. Apache2.2 + PHP + MySQL安裝配置
  2. AMFPHP1.9正式版配置
本文環境會如下
一、本文目前配置環境
OS : Windows XP
WebServer : Apache 2.2
PHP : PHP5.2 (本來使用5.3,發生回呼錯誤,先降為5.2待查5.3 bug)。
DataBase : MySQL 5.1
二、本文上個別系統配置資料夾
Apache的DocumentRoot : C:\Web Site,即網站資料夾,對應URL就是(本機上)Http://localhost/。AMFPHP1.9依官方建議資料夾改名為flashservices置放於C:\Web Site\flashservices

  • 網站資料夾建議改成webSite,不要有空白比較好,若修改記得去修改apache的httpd.conf檔。
Remote測試開始
Step1.建立資料夾
於AMFPHP Remote服務所在路徑 flashservices\services\amfphp新增資料夾結構為 tw\remote\test,新增完畢你會有如下路徑結構 flashservices\services\amfphp\tw\remote\test

Step2.建立一個Server端回呼程式
1.編輯一個Hello.php檔如下,編輯完存放於flashservices\services\amfphp\tw\remote\test
2.可以使用notepad++工具來代替筆記本做簡單編輯。
//Server端回呼程式
//請用手打,記得存成UTF-8,若用notepad++在編碼選項中,還可選擇是否檔頭要含BOM。
<?php
class Hello
{
    public function sayHello($name = null)
   {
       return "Hi $name wellcome JBlog!";
   }
}
?>

Step3.更改flashservices底下的gateway.php檔,以支援中文字碼
這段底下的註解是告訴你設法
* Oriental languages (Chinese, japanese, korean):
$gateway->setCharsetHandler( "none", "ISO-8859-1", "ISO-8859-1" ); $gateway->setCharsetHandler( "iconv", "UTF-8", "UTF-8" );
$gateway->setCharsetHandler( "iconv", "big5", "big5" );
$gateway->setCharsetHandler( "iconv", "CP950", "CP950" );
$gateway->setCharsetHandler( "iconv", "Shift_JIS", "Shift_JIS" );
$gateway->setCharsetHandler( "iconv", "CP932", "CP932" );
$gateway->setCharsetHandler( "iconv", "CP949", "CP949" );
找到 $gateway->setCharsetHandler( "none", "ISO-8859-1", "ISO-8859-1" );
改成    $gateway->setCharsetHandler( "utf8_decode", "UTF-8", "UTF-8" );
存檔,記得要存程沒有BOM的UTF-8格式,不然會出錯
Step4.用瀏覽器測試Server端回呼程式有無運作
瀏覽器中輸入http://127.0.0.1/flashservices/browser/















點開左側tw.remote.test,點擊Hello,於name中輸入綾瀨遙,按下Call呼叫Method執行。

結果:















Step5.撰寫Flex的Remote服務
<?xml version="1.0" encoding="utf-8"?;>
<mx:Application applicationComplete="main();" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.Operation;
import mx.rpc.remoting.RemoteObject;
import mx.messaging.channels.AMFChannel;
import mx.messaging.ChannelSet;
private var _channelSet:ChannelSet;
private var _amfChannel:AMFChannel;
private var _remoteObj:RemoteObject;
private var _op:Operation;
private var _uri:String;
private var _channelSetID:String;
private var _source:String;
private var _name:String;
private var _destination:String;
private var _btnEnable:Boolean;
private function main():void
{
 iniRemoteProperty();
 iniRemoteObj();
 _btnEnable = true;
}
private function iniRemoteProperty():void
{
 //服務器位址
 _uri = "http://192.168.11.37/flashservices/gateway.php";
 //AMFPHP使用之通道ID
 _channelSetID = "my-amfphp";
 //要呼要操作的Class來源,由services之後算起
 _source = "tw.remote.test.Hello";
 //要呼叫的method
 _name = "sayHello";
 //AMFPHP此項不影響
 _destination = "my-amfphp";
}
private function iniRemoteObj():void
{
 _channelSet = new ChannelSet();
 _amfChannel = new AMFChannel(_channelSetID , _uri);
 _channelSet.addChannel(_amfChannel);
 _remoteObj = new RemoteObject();
 _remoteObj.destination = _destination;
 _remoteObj.channelSet = _channelSet;
 _remoteObj.source = _source;
 _op = new Operation();
 _op.name = _name;
 
 _remoteObj.addEventListener(ResultEvent.RESULT , onResult);
 _remoteObj.addEventListener(FaultEvent.FAULT , onFault);
 _remoteObj.operations = {sayHello:_op};
}
public function AMFPHPService():void
{
 if(_btnEnable == false)return;
 _remoteObj.sayHello(input.text);
}
private function onResult(e:ResultEvent):void
{
 trace("成功");
 output.text = e.result.toString() ;
}
private function onFault(e:FaultEvent):void
{
 trace("失敗");
}

]]>
</mx:Script>
 <mx:Panel width="300" height="200" verticalAlign="middle">
  <mx:HBox>
   <mx:Label text="回傳結果"/>
   <mx:TextInput id="output"/>
  </mx:HBox>
  <mx:HBox>
   <mx:Label text="輸入數值"/>
   <mx:TextInput id="input"/>
  </mx:HBox>
  <mx:Button id="sendBtn" label="send" mouseDown="AMFPHPService()" />
 </mx:Panel>>
</mx:Application>
 
//執行結果  

沒有留言:

張貼留言