C#程序与Flash的接口

参照sourceforge的开源项目,数据传输采用WDDX方法把对象及其属性串行化,接收端则解串行化解析出相应的数据。

 

//****************************************************************************
//                       ______
//                    .-"      "-.
//                   /    AOL     /
//                  |              |
//                  |,  .-.  .-.  ,|
//                  | )(__/  /__)( |
//                  |/     //     /|
//        (@_       (_     ^^     _)
//   _     ) /_______/__|IIIIII|__/__________________________
//  (_)@8@8{}<________|-/IIIIII/-|___________________________>
//         )_/        /          /
//        (@           `--------`     AOL FLASH STUDIO.
//****************************************************************************
// @FileName SharpFlash.as
// @Package sharpflash
// @Description C#程序与flash交互的接口,采用Singleton模式,只允许一个接口
// @Author aol
// @Email jeremy1982@21cn.com
// @Create 2004.10.10
// @LastChange 2004.10.11
// @History
//****************************************************************************

class sharpflash.SharpFlash
{
        private static var _instance:SharpFlash;
        
        public static function get instance():SharpFlash
        {
                //trace(_instance);
                if (_instance == null)
                {
                        _instance = new SharpFlash();
                }
                return _instance;
        }
        
        ///
        
        private var _data:String;
        
        private var watch:Function;
        
        private var wddx:Wddx;
        private var callBackList:Array;
        
        
        //构造函数
        private function SharpFlash()
        {
                wddx = new Wddx();
                this.watch("data",onData);
        }
        
        //属性data,由C#来赋值,flash监视其值的变化
        public function get data():String
        {
                return _data;
        }
        
        public function set data(value:String):Void
        {
                _data = value;
        }
        
        //data变化处理方法
        function onData(prop:String, oldVal:String, newVal:String)
        {
                // _root.result.text = newVal;
                var xml:XML = new XML();
                xml.parseXML(newVal);
                
                var response_xml:XMLNode = xml.firstChild.firstChild;
                
                //回调函数ID
                var tmp:XMLNode = response_xml.childNodes[0].firstChild;
                
                // callback 函数在这个结点里面有一个或者两个参数
                // 通过解析从C#端发送的包的arg结点可以得出参数的数目
                var argCount:Number = response_xml.childNodes[1].childNodes.length;
                // _root.result.text = argCount;
                switch (argCount)
                {
                        case 1:
                        var arg1_wddx_xml:XML = new XML();
                        arg1_wddx_xml.parseXML(response_xml.childNodes[1].childNodes[0].firstChild);
                        var funcArgs = new Array(1);
                        funcArgs[0] = wddx.deserialize(arg1_wddx_xml);
                        
                        // 执行
                        callBackList[tmp].callback.apply(callBackList[tmp].scope, funcArgs);
                        
                        // debug 代码:
                        // _root.result.text = "1 Arg Result: [0] = " + funcArgs[0];
                        break;
                        
                        case 2:
                        var arg1_wddx_xml:XML = new XML();
                        arg1_wddx_xml.parseXML(response_xml.childNodes[1].childNodes[0].firstChild);
                        var arg2_wddx_xml:XML = new XML();
                        arg2_wddx_xml.parseXML(response_xml.childNodes[1].childNodes[1].firstChild);
                        var funcArgs:Array = new Array(2);
                        funcArgs[0] = wddx.deserialize(arg1_wddx_xml);
                        funcArgs[1] = wddx.deserialize(arg2_wddx_xml);
                        
                        // 执行
                        callBackList[tmp].callback.apply(callBackList[tmp].scope, funcArgs);
                        
                        // debug 代码:
                        //_root.result.text = "2 Arg Result: [0] = " + funcArgs[0] + "/n[1] = " + funcArgs[1];
                        break;
                        default:
                        break;
                }
        }
};
 
一个小示例,由C#得出当前程序所在路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值