RemoteProxy + Zend_Amf in AS3
2009年08月25日 22:07
Proxy class を extends した RemoteProxy class を作って Zend_Amf_Server にアクセス。
所謂、RemoteProxy Pattern を使用して RPC で Zend_Amf_Server 側の controller の関数をコール。
NetConnection オブジェクトで Zend_Amf_Server にアクセスする際に NetConnection.Connect.Success が NetStatusEvent で返ったのを確実に確認してから RPC しようと思ってたんだけど、NetConnection.Connect.Success って返らないんですね。NetConnection.Call.Failed などはきちんと返るんですが、接続成功時は NetStatusEvent の evt.info.code が何故か取得出来ない。
package com.convexstyle.proxy
{
dynamic public class CFZendAmfProxy extends Proxy implements IEventDispatcher
{
public function CFZendAmfProxy(gatewayUrl:String)
{
super();
if(gatewayUrl == null)
throw new CFProxyError("GatewayUrl is not provided in CFZendAmfProxy.");
this._gatewayUrl = gatewayUrl;
this.init();
}
private function init():void
{
this._eventDispatcher = new EventDispatcher(this);
this._responder = new Responder(onResultHandler, onStatusHandler);
this._nc = new NetConnection();
this._nc.objectEncoding = ObjectEncoding.AMF3;
this._nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandler, false, 0, true);
this._nc.connect(this._gatewayUrl);
}
private function onNetStatusHandler(evt:NetStatusEvent):void
{
switch(evt.info.code)
{
case "NetConnection.Connect.Success":
{
trace("@@@@@ Connected @@@@@");
break;
}
case "NetConnection.Connect.Failed":
case "NetConnection.Connect.Rejected":
case "NetConnection.Call.Failed":
case "NetConnection.Call.Prohibited":
{
this.dispatchEvent(new CFZendAmfProxyEvent(CFZendAmfProxyEvent.ERROR, null, {text: "Error: " + evt.info.code, code: evt.info.code}));
break;
}
default:
{
this.dispatchEvent(new CFZendAmfProxyEvent(CFZendAmfProxyEvent.ERROR, null, {text: "Error: " + evt.info.code, code: evt.info.code}));
break;
}
}
}
flash_proxy override function callProperty(method:*, ...params):*
{
this._call(method, params);
}
}
}
*一部省略
例えば、下記の様に debug メソッドをコールする際に、NetConnection.Connect.Success と debug() 関数のどちらが早くコールされるかによって処理を加えようと思ったのだけど、特に何もせずに正常に挙動するから特に気にする必要は無いのか!?Lightweight Remoting Framework のソースも参考までに確認してみたけど特に処理は無いな。
this._zendAmfProxy = new CFZendAmfProxy("http://www.test.com/amfserver/server.php");
this._zendAmfProxy.addEventListener(CFZendAmfProxyEvent.RESULT, onResultHandler, false, 0, true);
this._zendAmfProxy.addEventListener(CFZendAmfProxyEvent.STATUS, onStatusHandler, false, 0, true);
this._zendAmfProxy.addEventListener(CFZendAmfProxyEvent.ERROR, onErrorHandler, false, 0, true);
this._zendAmfProxy.UserController_debug();
ということは、多分特に気にしなくてもいいんだろう。
・・・・・
「参考サイト」
Zend_Amf について
NetConnection について
Proxy について
「お勧め本」
