守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 1140|回复: 0

[ActionScript] 基于SharedObject的LocalConnection

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52620
    钢镚
    1422

    开源英雄守望者

    发表于 2015-4-29 15:08:28 | 显示全部楼层 |阅读模式
    本帖最后由 破晓 于 2015-4-29 15:10 编辑

    昨天写了一个基于SharedObject的LocalConnection

    直接上代码

    1. package com.azri.localConn
    2. {
    3.         import flash.display.DisplayObject;
    4.         import flash.events.Event;
    5.         import flash.net.SharedObject;
    6.         import flash.utils.Dictionary;

    7.         /**
    8.          * 存放本地数据SharedObject
    9.          * */
    10.         public class LocalConnection
    11.         {
    12.                 private const SHARED_KEY :String = "LOCAL_CONNECTION_DATA";
    13.                 private static var _sharedObjects : SharedObject;
    14.    
    15.     private var _client:DisplayObject;
    16.    
    17.     private var _connKey:String;
    18.    
    19.     private var methodList:Dictionary = new Dictionary();
    20.    
    21.                 public function LocalConnection()
    22.                 {
    23.                 }
    24.    
    25.     public function connect(key:String):void
    26.     {
    27.       _connKey = key;
    28.       _client.addEventListener(Event.ENTER_FRAME, checkRequstHandler);
    29.     }
    30.    
    31.     public function colse():void
    32.     {
    33.       _client.removeEventListener(Event.ENTER_FRAME, checkRequstHandler);
    34.       _sharedObjects = null;
    35.       _client = null;
    36.     }
    37.    
    38.     public function registMethod(method:String, fun:Function):void
    39.     {
    40.       methodList[method] = fun;
    41.     }
    42.    
    43.     public function send(connectionName:String, methodName:String, ... arguments):void
    44.     {
    45.       var request:Array = getLocalData(_connKey);
    46.       request ||= [];
    47.       request.push({method:methodName, param:arguments});
    48.       setLocalData(connectionName, request);
    49.     }
    50.    
    51.     protected function checkRequstHandler(event:Event):void
    52.     {
    53.       var request:Array = getLocalData(_connKey);
    54.       var item:Object;
    55.       while(request && request.length > 0)
    56.       {
    57.         item = request.shift();
    58.         if(methodList.hasOwnProperty(item.method))
    59.           methodList[item.method].apply(null, item.param);
    60.         else if(item.method in _client)
    61.           _client[item.method].apply(null, item.param);
    62.       }
    63.       setLocalData(_connKey, null);
    64.     }
    65.    
    66.                 /**
    67.                  * 取值
    68.                  * @param key
    69.                  * @return
    70.                  *
    71.                  */               
    72.                 private function getLocalData(key :String) :*
    73.                 {
    74.                         if(_sharedObjects == null)
    75.                                 _sharedObjects = SharedObject.getLocal(SHARED_KEY, "/");
    76.                         if (_sharedObjects != null) {
    77.                                 return _sharedObjects.data[key];
    78.                         }
    79.                         
    80.                         return null;
    81.                 }
    82.                
    83.                 /**
    84.                  * 存值
    85.                  * @param key
    86.                  * @param value
    87.                  *
    88.                  */               
    89.                 public function setLocalData(key:String, value:*) :void
    90.                 {
    91.                         if(_sharedObjects == null)
    92.                                 _sharedObjects = SharedObject.getLocal(SHARED_KEY, "/");
    93.                         if (_sharedObjects != null) {
    94.                                 _sharedObjects.data[key] = value;
    95.                                 _sharedObjects.flush(500);
    96.                         }
    97.                 }

    98.     public function get client():DisplayObject
    99.     {
    100.       return _client;
    101.     }

    102.     public function set client(value:DisplayObject):void
    103.     {
    104.       _client = value;
    105.     }

    106.     public function get connKey():String
    107.     {
    108.       return _connKey;
    109.     }


    110.         }
    111. }
    复制代码


    使用案例:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
    3.                        xmlns:s="library://ns.adobe.com/flex/spark"
    4.                        xmlns:mx="library://ns.adobe.com/flex/mx"
    5.                        creationComplete="init(event)">
    6.   <fx:Script>
    7.     <![CDATA[
    8.       import com.azri.localConn.LocalConnection;
    9.       
    10.       import mx.events.FlexEvent;

    11.       
    12.       private var co1:com.azri.localConn.LocalConnection;
    13.       
    14.       protected function init(event:FlexEvent):void
    15.       {
    16.         co1 = new com.azri.localConn.LocalConnection();
    17.         co1.client = this;
    18.         co1.connect("test111");
    19.         
    20.         co1.registMethod("ok2", ok2);
    21.       }
    22.       
    23.       public function ok1(p:String):void
    24.       {
    25.         trace(p);
    26.       }
    27.       
    28.       public function ok2(p:String):void
    29.       {
    30.         trace(p);
    31.       }
    32.       
    33.       protected function dropdownlist1_mouseOverHandler(event:MouseEvent):void
    34.       {
    35.         trace("dsfgds");
    36.       }
    37.       
    38.       protected function button1_clickHandler(event:MouseEvent):void
    39.       {
    40.         var co:com.azri.localConn.LocalConnection = new com.azri.localConn.LocalConnection();
    41.         co.send("test111", "ok1", "aaa");
    42.       }
    43.       
    44.       protected function button2_clickHandler(event:MouseEvent):void
    45.       {
    46.         var co:com.azri.localConn.LocalConnection = new com.azri.localConn.LocalConnection();
    47.         co.send("test111", "ok2", "bbb");
    48.       }
    49.       
    50.     ]]>
    51.   </fx:Script>
    52.   <fx:Declarations>
    53.     <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    54.   </fx:Declarations>

    55.   <mx:Button label="ok1" click="button1_clickHandler(event)"/>
    56.   <mx:Button label="ok2" x="200" click="button2_clickHandler(event)"/>
    57. </s:WindowedApplication>
    复制代码



    还没仔细测试,有问题请回复


    源码:

    游客,如果您要查看本帖隐藏内容请回复

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-3-29 12:48 , Processed in 0.042247 second(s), 32 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表