AS3 remoting通讯

本文主要介绍如果实现AS3和C#联用的Flash remoting实现.后台的配置可以看《Flash/Flex/.net remoting通讯--Fluorine安装》。
    与Coldfusion使用remoting的简单相比,任何第三方后台都只能用麻烦来形容,可惜Coldfusion在中国。。。不提了。进正文。

    首先,此remoting非彼remoting,与.net中的remoting不同,这里的remoting指的是Flash通过AMF文件格式与后台的交互功能。Flash remoting是as1和as2中的组件,在Adobe的产品列表中有flash remoting mx组件,该组件封装了Flash中对于AMF的操作。而在As3中,虽然依然有AMF的操作,但是官方已经不再其实已经没有remoting的概念了,称呼为remoting了。

    从方法上来看,以前操作remoting的remoting包和rpc包也已经不再存在了。As3将主要的方法并入了NetConnection。而如rpc下的很多功能则被分了出来,如Responder类。详细可以看flash.net 包

    下例为一个简单的Hello world范例:

/*@
Author:frank
Site:www.2solo.cn
Date:2008.02.19
Info:remoting for .net with fluorinefx
*/

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import fl.controls.UIScrollBar;
    import flash..net.NetConnection;
    import flash..net.ObjectEncoding;
    import flash..net.Responder;


    public class amf3Csharp extends Sprite {
        public static var uiSb:UIScrollBar=new UIScrollBar();
        public static var btn=new test_btn;
        public static var labelTxt:TextField;
        public static var inputTxt:TextField;
        private static var nc:NetConnection = new NetConnection();

        public function amf3Csharp() {
            addMC();
            dispatchBtn(btn);
            setUpNC();
        }
        /*
        网络连接设定
        */

        private function setUpNC() {

            nc.client = nc;
            nc.objectEncoding = flash..net.ObjectEncoding.AMF3;
            nc.connect("http://localhost/Gateway.aspx");

        }
        /*
        场景元素定义按钮事件
        */

        private function addMC() {
            btn.x=30;
            btn.y=150;
            addChild(btn);
            labelTxt = new TextField();
            labelTxt.border = true;
            labelTxt.height=100;
            labelTxt.width=400;
            labelTxt.y=200;
            labelTxt.x=20;
            labelTxt.appendText("www.2solo.cn/log By frank");
            addChild(labelTxt);
            inputTxt= new TextField();
            inputTxt.multiline=true;
            inputTxt.border = true;
            inputTxt.height=100;
            inputTxt.width=400;
            inputTxt.y=20;
            inputTxt.x=20;
            inputTxt.type="input";
            inputTxt.text="Hello world!:)";
            addChild(inputTxt);
            uiSb.scrollTarget=labelTxt;
            uiSb.direction="vertical";
            uiSb.setSize(labelTxt.width,labelTxt.height);
            uiSb.move(labelTxt.x labelTxt.width,labelTxt.y);
            addChild(uiSb);
        }
        /*
        定义按钮事件
        */

        private function dispatchBtn(target):void {
            target.addEventListener(MouseEvent.CLICK,btnClick);
        }
        /*
        按钮事件
        */

        private function btnClick( event:Event ):void {
            labelTxt.text="发送Echo事件" "\n" labelTxt.text;
            nc.call("ServiceLibrary1.Sample.Echo",new Responder(SuccResult,OtherStatus),inputTxt.text);
            uiSb.update();
        }
        /*
        Remoting成功事件
        */

        private function SuccResult(re:String) {
            labelTxt.text=re "\n" labelTxt.text;
            trace("成功!");
        }
        /*
        Remoting失败事件
        */

        private function OtherStatus(re:Boolean) {
            trace("错误!");
        }
    }
}

其中Remoting方法的ServiceLibrary1.Sample.Echo路径可以通过Fluorine的控制台来取,如下图中,Service:ServiceLibrary1.Sample就是一个服务地址:

这个范例可以将上面的文本中的文字通过AMF发送到服务端的Echo方法,并将返回值输出到下面的文本中。

最后附上范例:http://www.2solo.cn/upload/amf1_2soloDOTcn.zip
载自:http://www.2solo.cn/

posted @ 2008-10-23 10:03  Landy_di  阅读(448)  评论(0)    收藏  举报