隐锋的BLOG
ASP,.net开发

shareObject的使用

要在RED5中使用远程sharedObject,必须注意以下几点:
1,
sharedObject = SharedObject.getRemote( “远程sharedObject名称“, nc.uri, true );
对借助服务器在多个客户端间共享的对象返回一个SharedObject的引用。
nc为NetConnection对象。
2,
sharedObject.client = this;
客户端对象为本身。便于广播消息,监听事件。
3,
sharedObject.connect( nc );
通过nc链接到服务器。
4,
在与flashplayer 9以前发布的FMS服务器进行sharedObject交互时,
一定要指定nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
否则flash无法监听到SYNC事件。目前使用的RED5服务器也是如此。
FLEX实例代码:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”initFun()”>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private var nc:NetConnection;
private var clientID:Number;
private var soChat:SharedObject;
private var arr:Array = new Array();
[Bindable]
private var con:ArrayCollection;
private function initFun():void
{
nc = new NetConnection();
nc.objectEncoding = flash.net.ObjectEncoding.AMF0;
nc.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
nc.connect( “rtmp://localhost/MySOSample” );
}
private function statusHandler(event:NetStatusEvent):void
{
if(event.info.code == “NetConnection.Connect.Success”){
connectToChat();
}
}
private function connectToChat():void
{
soChat = SharedObject.getRemote( “wxwred5″, nc.uri, true );
soChat.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
soChat.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
soChat.addEventListener( SyncEvent.SYNC, sharedObjectSyncHandler );
soChat.client = this;
soChat.connect( nc );
soChat.send( “getName” );
}
public function getName():void
{
trace( “getName: ” + txtUser.text );
soChat.setProperty(”key”,txtUser.text);
}
public function newName(str:String):void
{
arr.push(str);
soChat.setProperty(”arr”,arr);
con = new ArrayCollection(arr);
}
private function sharedObjectSyncHandler( event:SyncEvent ):void
{
trace( “sharedObjectSyncHandler:code: ” + event.changeList );
arr.push(event.target.data.key);
con = new ArrayCollection(arr);
}
private function netStatusHandler( event:NetStatusEvent ):void
{
trace( “netStatusHandler:code: ” + event.info.code );
}
private function asyncErrorHandler( event:AsyncErrorEvent ):void
{
trace( “asyncErrorHandler:code: ” + event.error );
}
]]>
</mx:Script>
<mx:TextInput id=”txtUser” horizontalCenter=”1″ verticalCenter=”-109″/>
<mx:Button click=”getName()” label=”Button” horizontalCenter=”0″ verticalCenter=”-53″/>
<mx:List id=”listView” height=”203″ dataProvider=”{con}” verticalCenter=”68″ horizontalCenter=”0″></mx:List>
</mx:Application>

posted on 2010-05-22 21:03  糊涂隐锋  阅读(1408)  评论(0编辑  收藏  举报