浏览 3876 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-15
原理和代码都很简单,没什么好说的.
一:利用ShareObject的同步事件来将消息广播到所有客户端:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical" initialize="BasicPracticeSO();"> <mx:Script> <![CDATA[ private var nc:NetConnection; private var shareObject:SharedObject; private var monthDay:Date; private var isConnectSuccess:Boolean; private static const RTMP_URL:String="rtmp://localhost/BasicSO"; private function BasicPracticeSO():void{ nc=new NetConnection; nc.connect(RTMP_URL); nc.addEventListener(NetStatusEvent.NET_STATUS,operateShareObject); } private function operateShareObject(evt:NetStatusEvent):void{ isConnectSuccess=(evt.info.code=="NetConnection.Connect.Success"); if(isConnectSuccess){ shareObject=SharedObject.getRemote("userInfo",nc.uri,false); monthDay=new Date(); shareObject.connect(nc); shareObject.addEventListener(SyncEvent.SYNC,detectSO); } } private function detectSO(e:SyncEvent):void{ for(var chng:uint;chng<e.changeList.length;chng++){ switch(e.changeList[chng].code){ case "change": updateAllClientMsg(); break; case "clear": break; case "success": break; default: break; } } } private function updateAllClientMsg():void{ soList.text+=shareObject.data.msg+"\n"; } private function addDataToShareObject():void{ shareObject.setProperty("msg",msgText.text); soList.text+=msgText.text+"\n"; msgText.text=""; } ]]> </mx:Script> <mx:Label text="ShareObject data:"/> <mx:TextArea id="soList" width="300" height="120"> </mx:TextArea> <mx:Form> <mx:FormHeading label="add data to ShareObject:"/> <mx:FormItem label="msg:"> <mx:TextInput id="msgText"/> </mx:FormItem> <mx:FormItem> <mx:Button label="send Msg" click="addDataToShareObject();"/> </mx:FormItem> </mx:Form> </mx:Application>
二:在FMS服务端通过编码方试广播消息到所有客户端:
1.客户端代码:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white" layout="vertical" initialize="initAppSO();"> <mx:Script> <![CDATA[ import com.client.ClientObj; private var nc:NetConnection; private var shareObject:SharedObject; private var monthDay:Date; private var isConnectSuccess:Boolean; private static const RTMP_URL:String="rtmp://localhost/BasicSO/chat"; private function initAppSO():void{ nc=new NetConnection; nc.connect(RTMP_URL); var clientObj:ClientObj=new ClientObj(soList); nc.client=clientObj; nc.addEventListener(NetStatusEvent.NET_STATUS,checkStatus); } private function checkStatus(evt:NetStatusEvent):void{ isConnectSuccess=(evt.info.code=="NetConnection.Connect.Success"); if(isConnectSuccess){ soList.text+="connect success"+"\n"; } } private function addDataToShareObject():void{ var msg:String=msgText.text; var responder:Responder=new Responder(callback); nc.call("writeFile",responder,msg); msgText.text=""; } private function callback(str:String):void{ // ToDo something } ]]> </mx:Script> <mx:Label text="chating list data:"/> <mx:TextArea id="soList" width="300" height="220"> </mx:TextArea> <mx:HBox> <mx:Label text="msgText:"/> <mx:TextInput id="msgText"/> <mx:Button label="send Msg" click="addDataToShareObject();"/> </mx:HBox> </mx:Application>
2.FMS服务器端代码:
这里我把聊天记录写到了服务器端的一个文件里.其实写不写都无所谓.
application.onAppStart=function(){ trace("app is started"); }; application.onConnect=function(client,name){ client.id=name; application.acceptConnection(client); var file=new File("chatfile.text"); client.writeFile=function(msg){ file.open("text","append"); if(file.isOpen){ msg=client.id+":"+client.ip+":say:\n"+msg; trace(msg); file.write(msg); file.close(); } //client.call("serverToClient",null,msg); for(var i=0;i<application.clients.length;i++){ application.clients[i].call("serverToClient",null,msg); } } }; application.onDisconnect=function(client){ for(var i=0;i<application.clients.length;i++){ msg=application.clients[i].id+" left!"; application.clients[i].call("serverToClient",null,msg); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |