How to use remote SharedObject in AS3 and Red5

2019-08-04 11:46发布

问题:

I wish to use remote SharedObject so I created a simple script to test out the techniques. When I ran the following code as two instances of SWF, both instances output 1, which was incorrect because the second instance was supposed to output 2.

import flash.net.SharedObject;
import flash.events.SyncEvent;

var nc:NetConnection; 
var so:SharedObject;

nc = new NetConnection(); 
nc.client = { onBWDone: function():void{} };
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 
nc.connect("rtmp://localhost:1935/live");

var t = new TextField();
addChild(t);

function onNetStatus(event:NetStatusEvent):void{ 
   if(event.info.code == "NetConnection.Connect.Success"){ 
      so = SharedObject.getRemote("shObj",nc.uri);
      so.connect(nc);
      if (!(so.data.total > 0 && so.data.total<1000)) {// undefined
          so.data.total=1;
      } else so.data.total=2;
      t.text=so.data.total;
   } 
}

Did I miss out something? Do I need to make some special settings to Flash or Red5? Do I need to create a special directory? Must I use a special event listener? Could anyone correct the code for me?


(09 Apr 2014)

When I used an event listener like the following, I got a blank screen for both instances, which was strange because I expected at least the second screen to show '2'. Can someone explain the behavior?

import flash.net.SharedObject;
import flash.events.SyncEvent;

var nc:NetConnection; 
var so:SharedObject;

nc = new NetConnection(); 
nc.client = { onBWDone: function():void{} };
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus); 
nc.connect("rtmp://localhost:1935/live");

var t = new TextField();
addChild(t);

function onNetStatus(event:NetStatusEvent):void{ 
   if(event.info.code == "NetConnection.Connect.Success"){ 
      so = SharedObject.getRemote("shObj",nc.uri);
      so.addEventListener(SyncEvent.SYNC,syncHandler);
      so.connect(nc);
      so.setProperty("total",2);
   } 
}

function syncHandler(event:SyncEvent):void{
   if (so.data.total) {
      t.text = so.data.total;
   }   
}

回答1:

Basically for the use of Shared Objects, I would recommend splitting the job into three seperate parts.

  1. Attach Event Listener and connect to the Shared Object

    function onNetStatus(event:NetStatusEvent):void
    { 
       if(event.info.code == "NetConnection.Connect.Success")
       { 
          so = SharedObject.getRemote("shObj",nc.uri);
          so.addEventListener(SyncEvent.SYNC,syncHandler); //add event listener for  Shared Object
          so.connect(nc);
       } 
    }
    
  2. Complete the Event Handler method to reflect changes in the value of Shared Object

    /*    This function is called whenever there is change in Shared Object data   */
    function syncHandler(event:SyncEvent):void
    {
        if(so.data.total) //if total field exists in the Shared Object
           trace(so.data.total);
    }
    
  3. Change the data in Shared Object:
    Use the setProperty method of Shared Object here. Invoke this method when you need to change the value (maybe at button click or on occurrence of certain Event)

    /*    This function writes values to the Shared Object    */
    function changeValue(newValue:String)
    {
       so.setProperty("total",newValue);
    }