package applications.amfExample
{
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetConnection;
import flash.net.ObjectEncoding;
import flash.net.Responder;
public class Amf
{
private static var conn:NetConnection = new NetConnection();
private static var requests:Array = new Array();
private static var netStatusError:Boolean = false;
private static var netStatusErrorMessage:String = "";
private static var netStatusErrorFunction:Function = null;
private static var resultFunction:Function = null;
private static var retry:int = 0;
private static var retried:int = 0;
/*
* Initialize AMF connection
* @conString:Server IP Address
* @retryNumber:Connection retried number on connect error
*/
public static function ini(conString:String, retryNumber:Number = 0, onErrorFunction:Function = null, onResultFunction:Function = null):void
{
conn.objectEncoding = ObjectEncoding.AMF3;
conn.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
conn.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
// Debug.info('[AMF] Connected to server : ' + conString);
conn.connect(conString);
retry = retryNumber;
resultFunction = onResultFunction;
netStatusErrorFunction = onErrorFunction;
}
public static function call(remoteFunction:String, localFunction:Function = null, object:Object = null):Number
{
if (null == object)object = new Object();
var request:Object = new Object();
request.remoteFunction = remoteFunction;
request.localFunction = localFunction != null ? localFunction : resultFunction;
request.object = object;
requests.push(request);
if (requests.length == 1)checkRequests();
return requests.length;
}
public static function checkRequests():void
{
if (requests.length > 0)
{
//Debug.info('[AMF] Amf call : ' + requests[0].remoteFunction);
conn.call(requests[0].remoteFunction, new Responder(onResult, onfault), requests[0].object);
}
}
private static function netStatusHandler(event:NetStatusEvent):void
{
if (retry > retried)
{
retried++;
checkRequests();
}
else
{
netStatusErrorMessage = event.info.code + ":" + requests[0].remoteFunction + "=>retried:" + retried;
onError();
}
}
private static function securityErrorHandler(event:SecurityErrorEvent):void
{
netStatusErrorMessage = "ERROR: SecurityErrorHandler: " + event;
onError();
}
private static function onResult(obj:Object):void
{
//Debug.info('[AMF] Amf callback : ' + requests[0].remoteFunction);
if(null == obj) obj = new Object();
try
{
if( undefined == obj.dataType) obj.dataType = requests[0].remoteFunction;
}
catch(e:Error)
{
netStatusErrorMessage = 'WARNING : There is no [dataType] after called on ' + requests[0].remoteFunction;
onError();
}
if (null != requests[0].localFunction)((requests[0].localFunction) as Function).call(requests[0].localFunction, obj);
netStatusError = false;
netStatusErrorMessage = "";
retried = 0;
requests.shift();
checkRequests();
}
private static function onfault(obj:Object):void
{
//netStatusErrorMessage = obj.toString();
netStatusErrorMessage = "ERROR: Server may not support *"+requests[0].remoteFunction + "*";
onError();
}
private static function onError():void
{
netStatusError = true;
if (null != netStatusErrorFunction)netStatusErrorFunction.call(netStatusErrorFunction, netStatusErrorMessage);
// Debug.info('[AMF] ' + netStatusErrorMessage);
}
public static function get con():NetConnection
{ //获取链接
return conn;
}
public static function close():void
{ //关闭链接
conn.close();
}
public static function getNetStatusErrorMessage():String
{ //获取错误信息
return netStatusErrorMessage;
}
public static function getNetStatusError():Boolean
{ //获取错误状态
return netStatusError;
}
}
}
[转] Amf通信类
最新推荐文章于 2024-04-18 21:34:34 发布