- 範例雖是Flex撰寫,但由於使用NetConnection來做Remote連線,所以Flash也適用。
example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application applicationComplete="init();" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
private var _btnEnable:Boolean;
private function init():void
{
_btnEnable = true;
initNetConnection();
}
private var _nc:NetConnection;
private function initNetConnection():void
{
_nc = new NetConnection();
//回呼處理物件
_nc.client = this;
//序列化格式指定
_nc.objectEncoding = ObjectEncoding.AMF3;
//指定AMFServer服務位置
_nc.connect(amfServerUri);
_nc.addEventListener(NetStatusEvent.NET_STATUS , onStatus);
}
private var amfServerUri:String="http://127.0.0.1/flashservices/gateway.php";
private var _source:String = "tw.remote.test.Hello.sayHello";
public function AMFPHPService():void
{
if(_btnEnable == false)return;
var _responder:Responder = new Responder(result , fault )
//呼叫method,設定回傳物件處理物件,傳入參數
_nc.call( _source , _responder , input.text );
}
private function onStatus(e:NetStatusEvent):void
{
}
private function result(e:Object):void
{
if(e != null)
output.text = e.toString();
}
private function fault(e:Object):void
{
output.text = "連線失敗";
}
]]>
</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>
- 將SWF發佈後,若放置WebServer上操作時發生安全性問題,記得在Remote Server上編寫crossdomain.xml檔案,這是允許SWF訪問的設定檔,可設定來自哪一個網域的SWF允許操作、讀取機器上的資料。
crossdomain.xml檔格式如下:
//這是允許所有網域的SWF來操作機器上的資料,但通常不會這麼設
沒有留言:
張貼留言