·您当前的位置:首页 > 技术教程 > RED5教程 >

[RED5]red5服务器端与客户端实例聊天室源代码

时间:2014-03-26 08:42cuplayer.com
ED5]red5服务器端与客户端实例,[RED5]red5服务器端与客户端实例聊天室源代码

ED5]red5服务器端与客户端实例

  1. import org.red5.server.adapter.ApplicationAdapter; 
  2. import org.red5.server.api.IConnection; 
  3. import org.red5.server.api.IScope; 
  4.  
  5. import org.red5.server.api.Red5; 
  6. import org.red5.server.api.service.IServiceCapableConnection; 
  7. import org.slf4j.Logger; 
  8. import org.red5.logging.Red5LoggerFactory; 
  9. import java.util.*; 
  10.  
  11.  
  12. public class Application extends ApplicationAdapter 
  13. { 
  14.     private static Logger log = Red5LoggerFactory.getLogger(Application.class, "echat"); 
  15.  
  16.      /** 
  17.     15      * 每个新的客户端来连接的时候调用! 
  18.     16      * 这里我们覆盖了父类的实现。 
  19.     17      */ 
  20.     public boolean appConnect(IConnection con, Object[] params) 
  21.     { 
  22.         log.debug("new client connectting chat room");        
  23.         return true; 
  24.     } 
  25.     
  26.     /** 
  27.     * 当客户端断开连接的时候调用! 
  28.     * 这里我们覆盖了父类的实现。 
  29.     */ 
  30.     public void appDisconnect(IConnection conn) 
  31.     { 
  32.         log.debug(conn.getClient().getId() + " disconnect"); 
  33.     } 
  34.     
  35.     
  36.     /** 
  37.     * 加入聊天室,必须带上用户名,假如用户名为空,则不能发送消息,也不能收到消息。 
  38.     * @param params 客户端调用服务器端的参数。 
  39.     */ 
  40.     public void jionChatRoom(Object[] params) 
  41.     { 
  42.         String nickName = params[0].toString(); 
  43.         if (null == nickName || "".equals(nickName)) 
  44.         { 
  45.             return ; 
  46.         } 
  47.         else 
  48.         { 
  49.             // CuPlayer.com提示:设置用户昵称。 
  50.             IConnection conn = Red5.getConnectionLocal(); 
  51.             conn.setAttribute("nickName", nickName); 
  52.         } 
  53.     
  54.          // 发通知给聊天室的所有人,有新人加入了。 
  55.         IScope scope = Red5.getConnectionLocal().getScope(); 
  56.         Iterator it =   scope.getConnections().iterator(); 
  57.         int x = 0; 
  58.         for (;it.hasNext();) 
  59.         { 
  60.             Set connections = (Set)it.next(); 
  61.             IConnection tempConn = (IConnection)connections.iterator().next(); 
  62.             if (tempConn instanceof IServiceCapableConnection) 
  63.             { 
  64.                 IServiceCapableConnection sc = (IServiceCapableConnection) tempConn; 
  65.                 // CuPlayer.com提示:服务器端调用客户端flash方法。 
  66.                 sc.invoke("showJoinInInfo", new Object[]{nickName}); 
  67.             } 
  68.         } 
  69.     } 
  70.     /** 
  71.     * 给聊天室的所有人发送消息 
  72.     * @param params 
  73.     */ 
  74.     public void sayToAll(Object[] params) 
  75.     { 
  76.         IConnection conn = Red5.getConnectionLocal(); 
  77.         //conn.setAttribute(”nickName”, nickName); 
  78.         String nickName =  conn.getAttribute("nickName").toString(); 
  79.         String sayWhat = params[0].toString(); 
  80.         // CuPlayer.com提示:发消息给聊天室的所有人. 
  81.         IScope scope = Red5.getConnectionLocal().getScope(); 
  82.         Iterator it = scope.getConnections().iterator(); 
  83.         for (;it.hasNext();) 
  84.         { 
  85.             Set connections = (Set)it.next(); 
  86.             IConnection tempConn = (IConnection)connections.iterator().next(); 
  87.             if (tempConn instanceof IServiceCapableConnection) 
  88.             { 
  89.                 IServiceCapableConnection sc = (IServiceCapableConnection) tempConn; 
  90.                 // CuPlayer.com提示:服务器端调用客户端flash方法。 
  91.                 sc.invoke("showMessage", new Object[]{nickName+ " : " +sayWhat}); 
  92.             } 
  93.         } 
  94.     } 
  95. } 

 

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
  3.  <mx:Script> 
  4.   <![CDATA[ 
  5.   
  6.    private var nc:NetConnection; 
  7.   
  8.    private function connectAndJoinRoom(e:Event):void 
  9.    { 
  10.         var nickName:String = nickNameTextInput.text; 
  11.         if (nickName == "") 
  12.         { 
  13.              return; 
  14.         } 
  15.         else 
  16.         { 
  17.              if (nc == null) 
  18.              { 
  19.               initialNetConnection(); 
  20.              } 
  21.         } 
  22.    } 
  23.    
  24.    private function initialNetConnection():void 
  25.    { 
  26.         nc = new NetConnection(); 
  27.         nc.addEventListener(NetStatusEvent.NET_STATUS, connectStatus); 
  28.         nc.client = this; 
  29.         nc.connect("rtmp://localhost/echat", null); 
  30.    } 
  31.    
  32.    private function connectStatus(event:NetStatusEvent) : void 
  33.    { 
  34.     if (event.info.code == "NetConnection.Connect.Success") 
  35.     { 
  36.      nc.call("jionChatRoom", null, nickNameTextInput.text); 
  37.      sendButton.enabled = true; 
  38.     } 
  39.    } 
  40.    
  41.    private function sendMessage():void 
  42.    { 
  43.     var sendString:String = inputWhatYouWantSay.text; 
  44.     inputWhatYouWantSay.text = ""; 
  45.     nc.call("sayToAll", null, sendString); 
  46.    } 
  47.    
  48.    public function showJoinInInfo(message:String) : void 
  49.    { 
  50.     roomArea.text += message + " 加入聊天室" + "/n"; 
  51.    } 
  52.    
  53.    public function showMessage(message:String) : void 
  54.    { 
  55.     roomArea.text += message + "/n"; 
  56.    } 
  57.   ]]> 
  58.  </mx:Script> 
  59.   
  60.   
  61.  <mx:VBox x="20" y="20"> 
  62.   <mx:HBox> 
  63.    <mx:Label text="昵称 : "></mx:Label> 
  64.    <mx:TextInput id="nickNameTextInput"> 
  65.     
  66.    </mx:TextInput> 
  67.    <mx:Button click="connectAndJoinRoom(event)" label="加入聊天室"> 
  68.     
  69.    </mx:Button> 
  70.   </mx:HBox> 
  71.   <mx:HBox width="100%" height="300"> 
  72.    <mx:TextArea height="100%" width="100%" id="roomArea"> 
  73.     
  74.    </mx:TextArea> 
  75.   </mx:HBox> 
  76.   <mx:HBox width="100%"> 
  77.    <mx:TextInput width="100%" id="inputWhatYouWantSay"> 
  78.    </mx:TextInput> 
  79.    <mx:Button enabled="false" id="sendButton" label="发送" click="sendMessage()"> 
  80.    </mx:Button> 
  81.   </mx:HBox> 
  82.  </mx:VBox> 
  83.  
  84. </mx:Application> 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器