Oct 27

as3的cookies访问

ActionScript3.0
FLASH 有2种 cookies
一种是传统浏览器中的cookies
一种是AS3 自带的
下面就把基于以上两种的Cookies类发布一下 很有用

浏览器COOKIES

package 
{
        import flash.external.ExternalInterface;

        public class CookieJar
        {
                private static const FUNCTION_SETCOOKIE:String =
        "document.insertScript = function ()" +
        "{ " +
          "if (document.snw_setCookie==null)" +
          "{" +
            "snw_setCookie = function (name, value, days)" +
            "{" +
              "if (days) {"+
                              "var date = new Date();"+
                              "date.setTime(date.getTime()+(days*24*60*60*1000));"+
                              "var expires = '; expires='+date.toGMTString();"+
                          "}" +
                          "else var expires = '';"+
                          "document.cookie = name+'='+value+expires+'; path=/';" +
                "}" +
          "}" +
        "}";

          private static const FUNCTION_GETCOOKIE:String =
        "document.insertScript = function ()" +
        "{ " +
          "if (document.snw_getCookie==null)" +
          "{" +
            "snw_getCookie = function (name)" +
            "{" +
              "var nameEQ = name + '=';"+
                          "var ca = document.cookie.split(';');"+
                          "for(var i=0;i < ca.length;i++) {"+
                              "var c = ca[i];"+
                              "while (c.charAt(0)==' ') c = c.substring(1,c.length);"+
                              "if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);"+
                          "}"+
                          "return null;" +
                "}" +
          "}" +
        "}";
      private static var INITIALIZED:Boolean = false;

      public static function init():void{
          ExternalInterface.call(FUNCTION_GETCOOKIE);
          ExternalInterface.call(FUNCTION_SETCOOKIE);
          INITIALIZED = true;
      }

      public static function setCookie(name:String, value:Object, days:int):void{
        if(!INITIALIZED)
        init();

        ExternalInterface.call("snw_setCookie", name, value, days);
      }

      public static function getCookie(name:String):Object{
        if(!INITIALIZED)
            init();

        return ExternalInterface.call("snw_getCookie", name);
      }

      public static function deleteCookie(name:String):void{
        if(!INITIALIZED)
            init();

        ExternalInterface.call("snw_setCookie", name, "", -1);
      }

        }
}


AS3自带的COOKIES 好像占用空间不超过1M

package emment.cookies {
  
  import flash.display.DisplayObject;
  import flash.display.DisplayObjectContainer;
  import flash.display.Sprite;
  import flash.display.Stage;
  import flash.events.KeyboardEvent;
  import flash.events.NetStatusEvent;
  import flash.net.SharedObject;
  import flash.net.SharedObjectFlushStatus;
  import flash.ui.Keyboard;

  public class FlashCookie {
  
    private var mCookieName : String = '_eat_more_cookies';
    private var _SharedObject : SharedObject;
    
    // SharedObject version num - useful if we need to change formats in future versions
    // upping this number will reset the shared object in all clients
    private var _version : Number = 1.0;
    
    private static var instance:FlashCookie;
    private static var allowInstantiation:Boolean;
    private var stage:Stage;
    
    public function FlashCookie ():void {
      if (!allowInstantiation) {
        throw new Error("Error: Instantiation failed: Use FlashCookie.getInstance() instead of new.");
      }
    }
    
    public static function getInstance():FlashCookie {
      if (instance == null) {
        allowInstantiation = true;
        instance = new FlashCookie ();
        allowInstantiation = false;
        
        FlashCookie.getInstance().init();
      }
      return instance;
    }
    
    
    /**
    * Initialize cookie
    */
    private function init():void 
    {
      //trace( "FlashCookie.init" );
      
      _SharedObject = SharedObject.getLocal(mCookieName);
      // make sure we are not dealing with an older version of our flash cookie
      // if old version or not defined, set it up
      if(_SharedObject.data.version == undefined || _SharedObject.data.version < _version) {
        reset();
      }    
      
    }
    
    // secret shortcut key
    public function CONTROL_ALT_SHIFT (inStage:Stage) 
    {
      trace( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      trace( "+  FlashCookie.CONTROL_ALT_SHIFT > inStage : " + inStage );
      trace( "+  CONTROL/APPLE + ALT + SHIFT :: ARROW-LEFT  >> clear cookie" );
      trace( "+  CONTROL/APPLE + ALT + SHIFT :: ARROW-RIGHT >> set cookie" );
      trace( "++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
      
      inStage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
    }
    
    
    private function reportKeyDown(e:KeyboardEvent):void 
    {
      //trace( "FlashCookie.reportKeyDown > e : " + e );
      
      if ((e.keyCode == Keyboard.LEFT) && (e.altKey == true) && (e.shiftKey == true) && (e.ctrlKey == true)) {
        trace ('+  FlashCookie :: CLEAR sharedObject');
        reset();
        //traceData();
      }
      if ((e.keyCode == Keyboard.RIGHT) && (e.altKey == true) && (e.shiftKey == true) && (e.ctrlKey == true)) {
        trace ('+  FlashCookie :: SET sharedObject');
        FlashCookie.getInstance().saveValue(true, 'firstTime');
        traceData();
      }
      

    }
    
    
    public function saveValue (inValue:* , inNameValue:String = 'savedValue'):void 
    {
      // trace( "FlashCookie.saveValue > inNameValue : " + inNameValue + ", inValue : " + inValue );      
      
      _SharedObject.data[inNameValue] = inValue;
            
            var flushStatus:String = null;
            try {
                flushStatus = _SharedObject.flush(10000);
            } catch (error:Error) {
          trace ("Error...Could not write SharedObject to disk");
            }
            if (flushStatus != null) {
                switch (flushStatus) {
                    case SharedObjectFlushStatus.PENDING:
                        trace("Requesting permission to save object...");
                        _SharedObject.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
                        break;
                    case SharedObjectFlushStatus.FLUSHED:
                        trace("Value flushed to disk.");
                        break;
                }
            }
    }
    
    /**
    * Reset cookie var 
    * 
    * Override this and add necessary default values here
    */
    public function reset() 
    {
      //trace( "FlashCookie.reset" );
      _SharedObject.clear();
      _SharedObject.data.version = _version;
    }

    /**
    * Flush the cookies to disk
    * // writes changes to disk
    */
    public function flush() 
    {
        return _SharedObject.flush(10000);
    }
    
    /**
    * return cookie data
    * 
    * @return
    */
    public function get data () : Object {
      return _SharedObject.data;
    }
    
    
    public function traceData ():void 
    {
      trace( " - FlashCookie.traceData // start" );
      for ( var i:String in _SharedObject.data ) {
        trace( "\tkey : " + i + ", value : " + _SharedObject.data[ i ] );
      }
      trace( " - FlashCookie.traceData // end" );
    }
    
    public function getSize ():void
    {
      trace( "FlashCookie.getSize: " + _SharedObject.size); 
    }
    
    //////////////////////////////////////// Listeners ////////////////////////////////////////
    
    private function onFlushStatus(event:NetStatusEvent):void {
            trace ("User closed permission dialog...");
            switch (event.info.code) {
                case "SharedObject.Flush.Success":
                    trace ("User granted permission -- value saved.");
                    break;
                case "SharedObject.Flush.Failed":
          trace ("User denied permission -- value not saved.");
                    break;
            }
            _SharedObject.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
        }

    
  } // end class
  
}

tags:

to "as3的cookies访问"

Leave a Reply