actionscript 3 - How to use remote SharedObject in AS3 and Red5 -


i wish use remote sharedobject created simple script test out techniques. when ran following code 2 instances of swf, both instances output 1, incorrect because second instance supposed 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"){        = 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 miss out something? need make special settings flash or red5? need create special directory? must use special event listener? correct code me?


(09 apr 2014)

when used event listener following, got blank screen both instances, strange because expected @ least second screen show '2'. can explain 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"){        = 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;    }    } 

basically use of shared objects, recommend splitting job 3 seperate parts.

  1. attach event listener , connect shared object

    function onnetstatus(event:netstatusevent):void {     if(event.info.code == "netconnection.connect.success")    {        = sharedobject.getremote("shobj",nc.uri);       so.addeventlistener(syncevent.sync,synchandler); //add event listener  shared object       so.connect(nc);    }  } 
  2. complete event handler method reflect changes in value of shared object

    /*    function called whenever there change in shared object data   */ function synchandler(event:syncevent):void {     if(so.data.total) //if total field exists in shared object        trace(so.data.total); } 
  3. change data in shared object:
    use setproperty method of shared object here. invoke method when need change value (maybe @ button click or on occurrence of event)

    /*    function writes values shared object    */ function changevalue(newvalue:string) {    so.setproperty("total",newvalue); } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -