如何使用远程共享对象在AS3和Red5的 [英] How to use remote SharedObject in AS3 and Red5
问题描述
我希望使用远程共享对象,所以我创建了一个简单的脚本来测试该技术。当我跑到下面的code为SWF的两个实例,这两种情况下输出1,这是不正确的,因为第二个实例应该输出2。
进口flash.net.SharedObject在;
进口flash.events.SyncEvent;
VAR数控:的NetConnection;
VAR因此:共享对象;
NC =新的NetConnection();
nc.client = {onBWDone:函数():无效{}};
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
nc.connect(RTMP://本地主机:1935 /活);
变种T =新的TextField();
的addChild(T);
功能onNetStatus(事件:将NetStatusEvent):无效{
如果(event.info。code ==值为NetConnection.Connect.Success){
所以= SharedObject.getRemote(shObj,nc.uri);
so.connect(NC);
如果((so.data.total>!0安培;&安培; so.data.total&所述; 1000)){//未定义
so.data.total = 1;
}否则so.data.total = 2;
t.text = so.data.total;
}
}
我错过了一些东西?我是否需要进行一些特殊的设置,以Flash或Red5的?我需要创建一个特殊的目录?我必须用一个特殊的事件监听器?任何人都可以纠正code我?
(2014年4月9日)
当我用事件侦听器,如下面,我得到了一个空白页面的两个实例,这很奇怪,因为我预计至少第二屏幕来显示2。有人可以解释的行为?
进口flash.net.SharedObject在;
进口flash.events.SyncEvent;
VAR数控:的NetConnection;
VAR因此:共享对象;
NC =新的NetConnection();
nc.client = {onBWDone:函数():无效{}};
nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
nc.connect(RTMP://本地主机:1935 /活);
变种T =新的TextField();
的addChild(T);
功能onNetStatus(事件:将NetStatusEvent):无效{
如果(event.info。code ==值为NetConnection.Connect.Success){
所以= SharedObject.getRemote(shObj,nc.uri);
so.addEventListener(SyncEvent.SYNC,syncHandler);
so.connect(NC);
so.setProperty(总,2);
}
}
功能syncHandler(事件:SyncEvent):无效{
如果(so.data.total){
t.text = so.data.total;
}
}
基本上是使用共享对象,我会建议分拆工作分为三个独立的部分。
-
附加事件监听器并连接到共享对象
函数onNetStatus(事件:将NetStatusEvent):无效 { 如果(event.info。code ==值为NetConnection.Connect.Success) { 所以= SharedObject.getRemote(shObj,nc.uri); so.addEventListener(SyncEvent.SYNC,syncHandler); //添加事件侦听器共享对象 so.connect(NC); } }
-
完成事件处理方法,以反映变化的共享对象的值
/ *这个函数被调用每当有变化,共享对象的数据* / 功能syncHandler(事件:SyncEvent):无效 { 如果(so.data.total)//如果总场存在的共享对象 跟踪(so.data.total); }
-
更改数据的共享对象:
使用<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html#setProperty%28String,Object%29"相对=nofollow>的SetProperty
共享对象的方法在这里。当你需要更改值(也许在按一下按钮或发生某些事件)调用此方法/ *该函数将值共享对象* / 功能changeValue(为newValue:字符串) { so.setProperty(总,为newValue); }
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;
}
}
Basically for the use of Shared Objects, I would recommend splitting the job into three seperate parts.
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); } }
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); }
Change the data in Shared Object:
Use thesetProperty
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); }
这篇关于如何使用远程共享对象在AS3和Red5的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!