[FMS]一步一步学Flash Media Server(五)
今天我们的讲解的是在昨天代码的功能上加上在线列表的功能,同时会去掉共享对象,用另一种方法向客户端发消息. 先看看服务端代码我们更改些什么代码:
application.onAppStart?
=
?function()?{
???? this .chatMsgArray? ? new ?Array(); ???? .userListArray? ?Array(); } application.onConnect? ?function(client,?userName)?{ ???? if (checkOnline(userName)){ ???????? .rejectConnection(client); ???????? return ; ????} ???? .acceptConnection(client); ????client.userName? ?userName; ???? .userListArray.push(userName); ????sendUserList(); ???? // 客户端调用方法 ????client.getMsg? ?function(){ ???????? ?application.chatMsgArray; ????} ????client.sendMsg? ?function(msg){ ????????var?chatInfo? .userName? + " ?:? ?msg; ????????application.chatMsgArray.push(chatInfo); ????????sendMsgToClient(chatInfo); ????} } application.onDisconnect? ?function(client)?{ ????trace( 用户: client.userName ?离开 ); ????var?len? .userListArray.length; ???? for (var?i 0 ;i < len;i ++ ){ ???????? ( .userListArray[i]? == ?client.userName){ ???????????? .userListArray.splice(i, 1 ); ????????????sendUserList(); ????????} ????} } application.onAppStop? ?function()?{ ????delete? .chatMsg_so; } function?checkOnline(userName){ ????var?len? ?application.userListArray.length; ???? (application.userListArray[i]? ?userName){ ???????????? true ; ????????} ????} ???? false ; } function?sendMsgToClient(chatInfo){ ????var?len? ?application.clients.length; ???? ){ ????????application.clients[i].call( getMsgInfo ,255)">null getUserList ? this.chatMsgArray = new Array(); 这两个定义的数组是在开始定义的,chatMsgArray是存储所有的聊天信息,userListArray是一个存储在线列表的数组 sendUserList函数是向所以客户端发送在线列表信息,application.clients是一个存储所以客户端连接的信息,application.clients.call就是调用客户端函数,使用方法跟客户端调服务器端是一样的. 然后我们增加了1个供客户端调用的函数 client.getMsg = function(){ client.getMsg是返回所有的聊天信息,当用户第一次连接时,得到别人的聊天记录 在用户断开连接的时候增加了将断线用户从在线列表中清除,并且发送在线列表. 再来看看客户端代码: ? package
?net.smilecn.chat{
???? ???? import ?flash.display.Sprite; ???? ?flash.net.NetConnection; ???? ?flash.net.Responder; ???? ?flash.events.NetStatusEvent; ???? ?flash.events.SyncEvent; ???? ?flash.events.MouseEvent; ???? ?fl.controls.TextArea; ?????? ?fl.controls.Button; ?????? ?fl.controls.TextInput; ???? ?fl.controls.Label; ???? ?fl.controls.List; ???? ?fl.data.DataProvider; ???? ???? public class ?Chat? extends ?Sprite{ ???????? ???????? private ?var?nc:NetConnection; ???????? ?var?rtmpUrl:String? rtmp://localhost/chat ; ???????? ?var?msg:Label; ???????? ?var?userNameInput:TextInput; ???????? ?var?enterBtn:Button; ???????? ?var?button:Button; ???????? ?var?textArea:TextArea; ???????? ?var?textInput:TextInput; ???????? ?var?userList:List; ???????? ???????? ?var?userName:String? user002 ; ???? ???????? ?function?Chat(): void { ????????????userNameInput? ?TextInput(); ????????????userNameInput.move( 100 200 ); ????????????addChild(userNameInput); ????????????enterBtn? ?Button(); ????????????enterBtn.move( 220 ); ????????????enterBtn.label? 进入 ; ????????????addChild(enterBtn); ????????????enterBtn.addEventListener(MouseEvent.CLICK,enterBtnClickHandler); ????????????msg? ?Label(); ????????????msg.move( 230 ); ????????????msg.text? "" ; ????????????addChild(msg); ????????} ???????? ???????? ?function?enterBtnClickHandler(event:MouseEvent): { ???????????? (userNameInput.text != ){ ????????????????userName? ?userNameInput.text; ????????????????removeChild(userNameInput); ????????????????removeChild(enterBtn); ????????????????removeChild(msg); ???????????? ????????????????textArea ?TextArea(); ????????????????textArea.setSize?( 300 ); ????????????????textArea.move?( 20 ); ????????????????addChild?(textArea); ????????????????textInput ?TextInput(); ????????????????textInput.width? 140 ; ????????????????textInput.move?( 330 ); ????????????????addChild?(textInput); ???????????????????button ?Button(); ????????????????button.width 50 ; ???????????????????button.label 发送 ; ???????????????????button.move?( 170 ); ????????????????addChild(button); ????????????????button.addEventListener?(MouseEvent.CLICK,sendMsg); ???????????????? ????????????????userList? ?List(); ????????????????userList.move( 250 ); ????????????????userList.setSize( ); ????????????????addChild(userList); ???????????? ????????????????nc ?NetConnection(); ????????????????nc.addEventListener?(NetStatusEvent.NET_STATUS,netStatusHandler); ????????????????nc.connect?(rtmpUrl,userName); ????????????????nc.client? ; ????????????} else { ????????????????msg.text? 请输入用户名 ; ????????????} ???????????? ????????} ???????? ???????? ?function?netStatusHandler(event:NetStatusEvent): { ????????????trace( event.info.code: (event.info.code? NetConnection.Connect.Success ){ ????????????????nc.call( getMsg ?Responder(getMsgResult,getMsgFault)) ????????????} ????????} ???????? ???????? ?function?getMsgResult(re:Array): { ????????????var?s:String ; ????????????var?len:Number? ?re.length; ???????????? ){ ????????????????s? += ?re[i] ; ????????????} ????????????textArea.text? ?s; ????????} ???????? ???????? ?function?getMsgFault(fe: * ): { ????????????trace(fe); ????????} ???????? ???????? ?function?sendMsg?(e:MouseEvent): { ????????????nc.call( sendMsg ; ????????} ???????? ???????? ?function?getMsgInfo(msg:String): { ????????????textArea.appendText(msg ); ????????} ???????? ???????? ?function?getUserList(list:Array): { ????????????userList.dataProvider? ?DataProvider(list); ????????} ????} ???? } 客户端首先增加了一个用户输入名字,这是些简单的代码. 然后共享对象的那部份去掉了,加了句这样的代码:nc.client = this; 这句话的意思是指定当前对象为服务器回调方法的对象 public function getMsgInfo(msg:String):void{ 这两个方法就是服务器调用的方法. 在nc连接成功的地方加了句nc.call("getMsg",new Responder(getMsgResult,getMsgFault)),这句是在刚连接成功的时候得到以前用户的聊天记录. 顺便说一句,客户端代码用到了组件,所以要把这些组件放到库中才能运行,上一节也是这样的. 转载:http://blog.csdn.net/arrowyoung/article/details/2448919 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |