- 积分
- 136116
- 注册时间
- 2014-12-27
- 最后登录
- 2024-3-28
- 在线时间
- 602 小时
- 威望
- 562
- 贡献
- 29
- 金币
- 52620
- 钢镚
- 1422
- 交易凭证
- 1
- 分享
- 0
- 精华
- 33
- 帖子
- 2094
- 主题
- 1742
TA的每日心情 | 擦汗 2018-4-10 15:18 |
---|
签到天数: 447 天 [LV.9]以坛为家II
超级版主
- 威望
- 562
- 贡献
- 29
- 金币
- 52620
- 钢镚
- 1422
|
本帖最后由 破晓 于 2015-4-29 15:10 编辑
昨天写了一个基于SharedObject的LocalConnection
直接上代码
- package com.azri.localConn
- {
- import flash.display.DisplayObject;
- import flash.events.Event;
- import flash.net.SharedObject;
- import flash.utils.Dictionary;
- /**
- * 存放本地数据SharedObject
- * */
- public class LocalConnection
- {
- private const SHARED_KEY :String = "LOCAL_CONNECTION_DATA";
- private static var _sharedObjects : SharedObject;
-
- private var _client:DisplayObject;
-
- private var _connKey:String;
-
- private var methodList:Dictionary = new Dictionary();
-
- public function LocalConnection()
- {
- }
-
- public function connect(key:String):void
- {
- _connKey = key;
- _client.addEventListener(Event.ENTER_FRAME, checkRequstHandler);
- }
-
- public function colse():void
- {
- _client.removeEventListener(Event.ENTER_FRAME, checkRequstHandler);
- _sharedObjects = null;
- _client = null;
- }
-
- public function registMethod(method:String, fun:Function):void
- {
- methodList[method] = fun;
- }
-
- public function send(connectionName:String, methodName:String, ... arguments):void
- {
- var request:Array = getLocalData(_connKey);
- request ||= [];
- request.push({method:methodName, param:arguments});
- setLocalData(connectionName, request);
- }
-
- protected function checkRequstHandler(event:Event):void
- {
- var request:Array = getLocalData(_connKey);
- var item:Object;
- while(request && request.length > 0)
- {
- item = request.shift();
- if(methodList.hasOwnProperty(item.method))
- methodList[item.method].apply(null, item.param);
- else if(item.method in _client)
- _client[item.method].apply(null, item.param);
- }
- setLocalData(_connKey, null);
- }
-
- /**
- * 取值
- * @param key
- * @return
- *
- */
- private function getLocalData(key :String) :*
- {
- if(_sharedObjects == null)
- _sharedObjects = SharedObject.getLocal(SHARED_KEY, "/");
- if (_sharedObjects != null) {
- return _sharedObjects.data[key];
- }
-
- return null;
- }
-
- /**
- * 存值
- * @param key
- * @param value
- *
- */
- public function setLocalData(key:String, value:*) :void
- {
- if(_sharedObjects == null)
- _sharedObjects = SharedObject.getLocal(SHARED_KEY, "/");
- if (_sharedObjects != null) {
- _sharedObjects.data[key] = value;
- _sharedObjects.flush(500);
- }
- }
- public function get client():DisplayObject
- {
- return _client;
- }
- public function set client(value:DisplayObject):void
- {
- _client = value;
- }
- public function get connKey():String
- {
- return _connKey;
- }
- }
- }
复制代码
使用案例:
- <?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"
- creationComplete="init(event)">
- <fx:Script>
- <![CDATA[
- import com.azri.localConn.LocalConnection;
-
- import mx.events.FlexEvent;
-
- private var co1:com.azri.localConn.LocalConnection;
-
- protected function init(event:FlexEvent):void
- {
- co1 = new com.azri.localConn.LocalConnection();
- co1.client = this;
- co1.connect("test111");
-
- co1.registMethod("ok2", ok2);
- }
-
- public function ok1(p:String):void
- {
- trace(p);
- }
-
- public function ok2(p:String):void
- {
- trace(p);
- }
-
- protected function dropdownlist1_mouseOverHandler(event:MouseEvent):void
- {
- trace("dsfgds");
- }
-
- protected function button1_clickHandler(event:MouseEvent):void
- {
- var co:com.azri.localConn.LocalConnection = new com.azri.localConn.LocalConnection();
- co.send("test111", "ok1", "aaa");
- }
-
- protected function button2_clickHandler(event:MouseEvent):void
- {
- var co:com.azri.localConn.LocalConnection = new com.azri.localConn.LocalConnection();
- co.send("test111", "ok2", "bbb");
- }
-
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <mx:Button label="ok1" click="button1_clickHandler(event)"/>
- <mx:Button label="ok2" x="200" click="button2_clickHandler(event)"/>
- </s:WindowedApplication>
复制代码
还没仔细测试,有问题请回复
源码:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|