如何在纯AS3应用程序中创建RemoteService

时间:2011-04-06 07:23:34

标签: actionscript-3 flex flex3

我想从纯AS3应用程序进行远程服务调用。 对于Pure AS3,RemoteObject类不可用。 有没有人可以帮我解决问题。

谢谢, SUDESH

2 个答案:

答案 0 :(得分:1)

这是我为此写的课程。我在每个项目中使用它,所以我不必再考虑它了。

public class ServiceCaller extends EventDispatcher
{
    private static var s__instance:ServiceCaller;

    private var m__netConnection:NetConnection;

    public function ServiceCaller() 
    {
        var l__tempConnection:NetConnection = new NetConnection();
        l__tempConnection.objectEncoding = ObjectEncoding.AMF3;
        m__netConnection = new NetConnection();
        m__netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
        m__netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        m__netConnection.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);       
    }

    public function connect(i__url:String):void
    {           
        m__netConnection.connect(Model.instance.config.gatewayUrl); 
    }

    private function netStatusHandler(i__event:NetStatusEvent):void 
    {
        trace(i__event.info.code);
    }

    private function statusHandler(i__object:Object):void
    {
        for (var l__key:String in i__object)
            trace(l__key, i__object[l__key]);
        CursorManager.removeBusyCursor();
    }

    private function securityErrorHandler(i__event:SecurityErrorEvent):void 
    {
        throw new Error("securityErrorHandler: " + i__event.toString());
    }

    private function ioErrorHandler(i__event:IOErrorEvent):void 
    {
        throw new Error("ioErrorHandler: " + i__event.toString());
    }

    public function callService(i__name:String, i__listener:Function, ...arguments):void
    {
        m__netConnection.call.apply(m__netConnection, [i__name, new Responder(i__listener, statusHandler)].concat(arguments));
    }


    static public function get instance():ServiceCaller 
    {           
        if (!s__instance)
            s__instance = new ServiceCaller();
        return s__instance; 
    }

    static public function set instance(value:ServiceCaller):void 
    {
        s__instance = value;
    }

}

要使用它,您必须先拨打ServiceCaller.instance.connect("gatewayURL");一次,然后每次都要打电话:

ServiceCaller.instance.callService("serviceName", onResult, param1, param2);

onResult是一个回调函数,它将结果作为参数获取。您可以根据服务需要添加任意数量的参数。

答案 1 :(得分:0)

只需使用flash.net.NetConnection ...检查call()方法以及flash.net.Responder类。