• flex flashplayer 程序 和 air 程序 通过 LocalConnection 通信


    flashplayer 做控制端:

    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init(event)">
        <fx:Script>
            <![CDATA[
                import mx.events.FlexEvent;
                
                
                private var conn:LocalConnection;
                
                protected function init(event:FlexEvent):void
                {
                    conn = new LocalConnection;
                    conn.addEventListener(StatusEvent.STATUS,statusHandler);                
                }
                
                private function statusHandler(evt:StatusEvent):void
                {
                    switch (evt.level) {
                        case "status":
                            trace("LocalConnection.send() succeeded");
                            break;
                        case "error":
                            trace("LocalConnection.send() failed");
                            break;
                    }
                }
                
                protected function pressedHandler(event:MouseEvent):void
                {
                    var obj:Object = {name:txt.text,age:30};
                    conn.send('_connectionName3','methodName',obj);                
                }
                
            ]]>
        </fx:Script>
        <s:layout>
            <s:VerticalLayout/>
        </s:layout>
        <s:TextInput id="txt"/>
        <s:Button label="test" click="pressedHandler(event)"/>
    </s:Application>

    air 做客户端:

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx" close="windowedapplication1_closeHandler(event)"
                           creationComplete="init(event)">
        <fx:Script>
            <![CDATA[
                import mx.controls.Alert;
                import mx.events.FlexEvent;
                
                private var conn:LocalConnection;
                
                protected function init(event:FlexEvent):void
                {
                    conn = new LocalConnection;
                    conn.client = this;
                    
                    conn.allowDomain("*");
                    
                    try {
                        conn.connect('_connectionName3');
                        conn.addEventListener(StatusEvent.STATUS,statusHandler);
                    } catch (error:ArgumentError) {
                        trace("Can't connect...the connection name is already being used by another SWF");
                    }
                }
                
                private function statusHandler(evt:StatusEvent):void
                {
                    switch (evt.level) {
                        case "status":
                            trace("LocalConnection.send() succeeded");
                            break;
                        case "error":
                            trace("LocalConnection.send() failed");
                            break;
                    }
                }
                
                public function methodName(param:Object):void
                {
                    if(param)
                    {
                        Alert.show("姓名:" + param.name);
                    }
                }
                
                protected function windowedapplication1_closeHandler(event:Event):void
                {
                    if (conn)
                        conn.close();
                }
                
            ]]>
        </fx:Script>
        
    </s:WindowedApplication>

     client 对象绑定的对象可以是动态或普通类,其中中要访问的方法一定要是public的,或者直接绑定的类似这样:

    conn.client = {methodName:function(o:Object):void{
      Alert.show(o.name);
    }};

  • 相关阅读:
    金蝶问题:不能保存已审核的单据
    在代码中设置cxTreeList按多列排序
    日积月累篇:生产任务单
    sp_reset_connection
    日积月累篇:仓库流程
    使用FreeMarker生成Word文档 仅此而已
    ASP格式化时间日期(二)
    省市区三级联动连接数据库
    利用SQL语句进行添加、删除、修改字段,表与字段的基本操作,数据库备份等
    ASP截取字数(二)
  • 原文地址:https://www.cnblogs.com/xuezizhenchengxuyuan/p/5647237.html
Copyright © 2020-2023  润新知