asp网站怎么做,承接网站网站建设,wordpress如何添加表格,中国楼市未来发展趋势FMS开发中#xff0c;经常会使用共享对象来同步用户和存储数据。对于实现广播文字信息实现聊天的支持非常强大#xff0c;还可以跟踪用户的时时动作#xff0c;在开发Flash多人在线游戏中的应用也非常广阔。 在使用FMS开发共享对象时需要注意#xff0c;只有使用Flash Medi… FMS开发中经常会使用共享对象来同步用户和存储数据。对于实现广播文字信息实现聊天的支持非常强大还可以跟踪用户的时时动作在开发Flash多人在线游戏中的应用也非常广阔。 在使用FMS开发共享对象时需要注意只有使用Flash Media Interactive Server或Flash Media Development Server这两个版本时才能够创建和使用远程共享对象来实现多客户端的应用程序之间共享数据。如果是使用的Flash Media Streaming Server版FMS是不能创建远程共享对象的只能创建本地共享对象类似于传统Web开发中的Cookie。 使用共享对象(SharedObject)来开发时时文字聊天其实是很简单的SharedObject可以跟踪和广播消息连接到SharedObject中的其中任何一个客户端改变了SharedObject中的数据SharedObject就会将最新的数据广播到连接到它的所有客户端。从某种角度可以理解为远程的SharedObject是一个同步很多用户的一个网络中心。下图为官方发布的SharedObject广播消息图 本文是通过实现一个简单的文字聊天来介绍FMS中的远程共享对象的使用首先在FMS中建立好应用程序名既在FMS的安装目录下的applications下建立一文件夹来作为共享对象应用程序使用如下图所示 如上图SharedObjectApp就是为实现聊天建立的一个FMS应用文件夹其下的sharedobjects/_definse_为成功创建远程对象后自动生成的目录。如果你所创建的为永久性的远程共享对象则在该目录下还将会有一个以.fso为扩展名的远程共享对象文件。 要创建远程共享对象首先需要连接到FMS应用然后通过SharedObject.getRemote()方法来完成远程共享对象的创建通过给远程共享对象添加同步事件监听远程共享对象里的数据一但发生改变就会自动触发该事件来实现同步数据。 private function onClick():void { nc new NetConnection(); nc.connect(rtmp://192.168.1.101/SharedObjectApp); nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler); }private function onNetStatusHandler(evt:NetStatusEvent):void { this.panChat.title(evt.info.code); if(evt.info.codeNetConnection.Connect.Success) { //创建一个远程共享对象 //参数:远程共享对象的名称 | 连接到的应用程序的URI | 远程共享对象是否为永久远程对象 so SharedObject.getRemote(RemotingSO,nc.uri,true); //将生成SO.fso //远程对象(SharedObject)同步事件的监听 so.addEventListener(SyncEvent.SYNC,onSyncHandler); //远程共享对象连接到服务器 so.connect(nc); } } 上面代码块实现了连接到FMS应用成功连接后便创建远程共享对象(RemotingSO),同时还为远程共享对象添加了同步事件监听通过onSyncHandler方法来处理事件。 在继续实现聊天功能前我们需要编写一个通用方法该方法提供将一个数组里的数据转移到另一个数组如下代码块 private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void { arrNew.removeAll(); for(var i:int0;iarrOld.length ;i) { arrNew.addItemAt(arrOld.getItemAt(i),i); } } 下面我们通过发送消息的流程开始首先是发送消息通过自定义Message类来封装消息内容 1 package flex.VO 2 { 3 public class Message 4 { 5 public var NickName:String; //用户呢称 6 public var Context:String; //消息内容 7 8 public function Message() 9 {10 }11 }12 } 在发送消息的时候通过此Message类来封装发送消息的数据然后将其发布到FMS中的远程共享对象更新远程共享对象中的数据。 private function onSend():void { var tempCollection:ArrayCollection new ArrayCollection(); if(so.data.msgCollection ! null) { convertArrayCollection(tempCollection,so.data.msgCollection as ArrayCollection); } var msg:Message new Message(); msg.NickName this.txtUser.text; msg.Context this.txtMessage.text; tempCollection.addItem(msg); //更新远程共享对象中的属性值 so.setProperty(msgCollection,tempCollection); this.txtMessage.text; } 实现了发送消息(将消息添加到远程共享对象并更新远程共享对象的属性值)如果有多个客户端连接到该远程共享对象这时就回触发远程共享对象的同步事件通过同步事件处理方法就可以将远程共享对象中的数据同步到客户端。如下代码块 private function onSyncHandler(evt:SyncEvent):void { if(so.data.msgCollection!null) { var tempCollection:ArrayCollection new ArrayCollection(); convertArrayCollection(tempCollection,so.data.msgCollection as ArrayCollection); this.msgText.text; for(var index:int0;indextempCollection.length;index) { var message:Object tempCollection.getItemAt(index); var displayMessage:String message.NickName说:message.Context; this.msgText.text displayMessage \n; } } } 如上便完成了整个文字聊天的功能开发主要应用到的技术点就是通过远程共享对象来同步用户数据。下面为完整的Flex端代码 完整的Flex端代码?xml version1.0 encodingutf-8?mx:Application xmlns:mxhttp://www.adobe.com/2006/mxml layoutabsolute fontSize12 mx:Script ![CDATA[ import mx.controls.Alert; import mx.collections.ArrayCollection; import flex.VO.Message; private var nc:NetConnection; private var so:SharedObject; private function onClick():void { nc new NetConnection(); nc.connect(rtmp://192.168.1.101/SharedObjectApp); nc.addEventListener(NetStatusEvent.NET_STATUS,onNetStatusHandler); } private function onNetStatusHandler(evt:NetStatusEvent):void { this.panChat.title(evt.info.code); if(evt.info.codeNetConnection.Connect.Success) { //创建一个远程共享对象 //参数:远程共享对象的名称 | 连接到的应用程序的URI | 远程共享对象是否为永久远程对象 so SharedObject.getRemote(RemotingSO,nc.uri,true); //将生成SO.fso //远程对象(SharedObject)同步事件的监听 so.addEventListener(SyncEvent.SYNC,onSyncHandler); //远程共享对象连接到服务器 so.connect(nc); } } private function onSyncHandler(evt:SyncEvent):void { if(so.data.msgCollection!null) { var tempCollection:ArrayCollection new ArrayCollection(); convertArrayCollection(tempCollection,so.data.msgCollection as ArrayCollection); this.msgText.text; for(var index:int0;indextempCollection.length;index) { var message:Object tempCollection.getItemAt(index); var displayMessage:String message.NickName说:message.Context; this.msgText.text displayMessage \n; } } } private function onSend():void { var tempCollection:ArrayCollection new ArrayCollection(); if(so.data.msgCollection ! null) { convertArrayCollection(tempCollection,so.data.msgCollection as ArrayCollection); } var msg:Message new Message(); msg.NickName this.txtUser.text; msg.Context this.txtMessage.text; tempCollection.addItem(msg); //更新远程共享对象中的属性值 so.setProperty(msgCollection,tempCollection); this.txtMessage.text; } private function convertArrayCollection(arrNew:ArrayCollection,arrOld:ArrayCollection):void { arrNew.removeAll(); for(var i:int0;iarrOld.length ;i) { arrNew.addItemAt(arrOld.getItemAt(i),i); } } ]] /mx:Script mx:Panel x22 y22 width482 height260 layoutabsolute idpanChat title文字聊天 mx:TextArea x0 y0 width100% height100% backgroundColor#FCDADA idmsgText/ mx:ControlBar mx:TextInput width53 idtxtUser/ mx:Label text说:/ mx:TextInput width195 idtxtMessage/ mx:Button labelSend clickonSend()/ mx:Button labelConnection fontWeightnormal clickonClick()/ /mx:ControlBar /mx:Panel /mx:Application 程序运行截图如下 图1----FMS状态图 图2----聊天客户端张三 图3----聊天客户端李四 如上图在FMS应用目录下创建了一后缀为.fso的文件这就是永久性的远程共享对象文件名。在使用远程共享的时候根据实际需求来确定是否使用永久性的远程共享对象一般做聊天应用我个人建议使用临时远程共享对象不生成.fso文件要存储聊天记录可以通过其他方式来保存。 详细大家可以查看官方提供的文档在FMS的安装目录下就有我的是D:\Adobe\Flash Media Server 3\documentation\flashmediaserver_AS3LR\index.html 本文就介绍于此如文中有什么问题请大家拍砖指正。本文示例源代码下载 版权说明 本文属原创文章欢迎转载其版权归作者和博客园共有。 作 者Beniao 文章出处http://beniao.cnblogs.com/ 或 http://www.cnblogs.com/