• 【设为首页】
  • 【收藏闪客居】
当前位置:主页>FLASH AS 编程>FMS>文章内容
  • 一步一步学Flash Media Server(五)

  • 来源:blog.smilecn.net 作者:arrowyoung 2008-08-08 【

今天我们的讲解的是在昨天代码的功能上加上在线列表的功能,同时会去掉共享对象,用另一种方法向客户端发消息.

先看看服务端代码我们更改些什么代码:

  1. application.onAppStart = function() {
  2.     this.chatMsgArray = new Array();
  3.     this.userListArray = new Array();
  4. }
  5.  
  6. application.onConnect = function(client, userName) {
  7.     if(checkOnline(userName)){
  8.         this.rejectConnection(client);
  9.         return;
  10.     }
  11.     this.acceptConnection(client);
  12.     client.userName = userName;
  13.     this.userListArray.push(userName);
  14.     sendUserList();
  15.     //客户端调用方法
  16.     client.getMsg = function(){
  17.         return application.chatMsgArray;
  18.     }
  19.     client.sendMsg = function(msg){
  20.         var chatInfo = this.userName + " : " + msg;
  21.         application.chatMsgArray.push(chatInfo);
  22.         sendMsgToClient(chatInfo);
  23.     }
  24. }
  25.  
  26. application.onDisconnect = function(client) {
  27.     trace("用户:"+client.userName+" 离开");
  28.     var len = this.userListArray.length;
  29.     for(var i=0;i<len;i++){
  30.         if(this.userListArray[i] == client.userName){
  31.             this.userListArray.splice(i,1);
  32.             sendUserList();
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38. application.onAppStop = function() {
  39.     delete this.chatMsg_so;
  40. }
  41.  
  42. function checkOnline(userName){
  43.     var len = application.userListArray.length;
  44.     for(var i=0;i<len;i++){
  45.         if(application.userListArray[i] == userName){
  46.             return true;
  47.         }
  48.     }
  49.     return false;
  50. }
  51.  
  52. function sendMsgToClient(chatInfo){
  53.     var len = application.clients.length;
  54.     for(var i=0;i<len;i++){
  55.         application.clients[i].call("getMsgInfo",null,chatInfo);
  56.     }
  57. }
  58.  
  59. function sendUserList(){
  60.     var len = application.clients.length;
  61.     for(var i=0;i<len;i++){
  62.         application.clients[i].call("getUserList",null,application.userListArray);
  63.     }
  64. }

this.chatMsgArray = new Array();
this.userListArray = new Array();

这两个定义的数组是在开始定义的,chatMsgArray是存储所有的聊天信息,userListArray是一个存储在线列表的数组

当用户连接进来的时候,我们用了一个函数checkOnline来检查用户是不是在在线列表中,如果在就用this.rejectConnection(client);拒绝这个连接,如果不在就接受这个连接并加到在线列表中

sendUserList函数是向所以客户端发送在线列表信息,application.clients是一个存储所以客户端连接的信息,application.clients.call就是调用客户端函数,使用方法跟客户端调服务器端是一样的.

然后我们增加了1个供客户端调用的函数

  1. client.getMsg = function(){
  2.       return application.chatMsgArray;
  3. }

client.getMsg是返回所有的聊天信息,当用户第一次连接时,得到别人的聊天记录

在用户断开连接的时候增加了将断线用户从在线列表中清除,并且发送在线列表.

再来看看客户端代码:

  1. package net.smilecn.chat{
  2.    
  3.     import flash.display.Sprite;
  4.     import flash.net.NetConnection;
  5.     import flash.net.Responder;
  6.     import flash.events.NetStatusEvent;
  7.     import flash.events.SyncEvent;
  8.     import flash.events.MouseEvent;
  9.     import fl.controls.TextArea;
  10.     import fl.controls.Button;
  11.     import fl.controls.TextInput;
  12.     import fl.controls.Label;
  13.     import fl.controls.List;
  14.     import fl.data.DataProvider;
  15.  
  16.    
  17.     public class Chat extends Sprite{
  18.        
  19.         private var nc:NetConnection;
  20.         private var rtmpUrl:String = "rtmp://localhost/chat";
  21.         private var msg:Label;
  22.         private var userNameInput:TextInput;
  23.         private var enterBtn:Button;
  24.         private var button:Button;
  25.         private var textArea:TextArea;
  26.         private var textInput:TextInput;
  27.         private var userList:List;
  28.        
  29.         private var userName:String = "user002";
  30.    
  31.         public function Chat():void{
  32.             userNameInput = new TextInput();
  33.             userNameInput.move(100,200);
  34.             addChild(userNameInput);
  35.             enterBtn = new Button();
  36.             enterBtn.move(220,200);
  37.             enterBtn.label = "进入";
  38.             addChild(enterBtn);
  39.             enterBtn.addEventListener(MouseEvent.CLICK,enterBtnClickHandler);
  40.             msg = new Label();
  41.             msg.move(100,230);
  42.             msg.text = "";
  43.             addChild(msg);
  44.         }
  45.        
  46.         private function enterBtnClickHandler(event:MouseEvent):void{
  47.             if(userNameInput.text!=""){
  48.                 userName = userNameInput.text;
  49.                 removeChild(userNameInput);
  50.                 removeChild(enterBtn);
  51.                 removeChild(msg);
  52.            
  53.                 textArea=new TextArea();
  54.                 textArea.setSize (200,300);
  55.                 textArea.move (20,20);
  56.                 addChild (textArea);
  57.  
  58.                 textInput=new TextInput();
  59.                 textInput.width = 140;
  60.                 textInput.move (20,330);
  61.                 addChild (textInput);
  62.  
  63.                 button=new Button();
  64.                 button.width=50;
  65.                 button.label="发送";
  66.                 button.move (170,330);
  67.                 addChild(button);
  68.                 button.addEventListener (MouseEvent.CLICK,sendMsg);
  69.                
  70.                 userList = new List();
  71.                 userList.move(250,20);
  72.                 userList.setSize(100,300);
  73.                 addChild(userList);
  74.            
  75.                 nc=new NetConnection();
  76.                 nc.addEventListener (NetStatusEvent.NET_STATUS,netStatusHandler);
  77.                 nc.connect (rtmpUrl,userName);
  78.                 nc.client = this;
  79.             }else{
  80.                 msg.text = "请输入用户名";
  81.             }
  82.            
  83.         }
  84.        
  85.         private function netStatusHandler(event:NetStatusEvent):void{
  86.             trace("event.info.code:",event.info.code);
  87.             if(event.info.code == "NetConnection.Connect.Success"){
  88.                 nc.call("getMsg",new Responder(getMsgResult,getMsgFault))
  89.             }
  90.         }
  91.        
  92.         private function getMsgResult(re:Array):void{
  93.             var s:String="";
  94.             var len:Number = re.length;
  95.             for(var i=0;i<len;i++){
  96.                 s += re[i]+" ";
  97.             }
  98.             textArea.text = s;
  99.         }
  100.        
  101.         private function getMsgFault(fe:*):void{
  102.             trace(fe);
  103.         }
  104.        
  105.         private function sendMsg (e:MouseEvent):void{
  106.             nc.call("sendMsg",null,textInput.text);
  107.             textInput.text = "";
  108.         }
  109.        
  110.         public function getMsgInfo(msg:String):void{
  111.             textArea.appendText(msg+" ");
  112.         }
  113.        
  114.         public function getUserList(list:Array):void{
  115.             userList.dataProvider = new DataProvider(list);
  116.         }
  117.     }
  118.    
  119. }

客户端首先增加了一个用户输入名字,这是些简单的代码.

然后共享对象的那部份去掉了,加了句这样的代码:nc.client = this;

这句话的意思是指定当前对象为服务器回调方法的对象

  1. public function getMsgInfo(msg:String):void{
  2.        textArea.appendText(msg+"\n");
  3. }
  4.  
  5. public function getUserList(list:Array):void{
  6.        userList.dataProvider = new DataProvider(list);
  7. }

这两个方法就是服务器调用的方法.

在nc连接成功的地方加了句nc.call(”getMsg”,new Responder(getMsgResult,getMsgFault)),这句是在刚连接成功的时候得到以前用户的聊天记录.

顺便说一句,客户端代码用到了组件,所以要把这些组件放到库中才能运行,上一节也是这样的.

下节继续.

(本教程如需转载请注明出处!)




上一篇:一步一步学Flash Media Server(四)   下一篇:一步一步学Flash Media Server(六)
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)