red5 服务器端与客户端实例

package echat;



import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;

import org.red5.server.api.Red5;
import org.red5.server.api.service.IServiceCapableConnection;
import org.slf4j.Logger;
import org.red5.logging.Red5LoggerFactory;
import java.util.*;


public class Application extends ApplicationAdapter
{
    private static Logger log = Red5LoggerFactory.getLogger(Application.class, "echat");

     /**
    15      * 每个新的客户端来连接的时候调用!
    16      * 这里我们覆盖了父类的实现。
    17      */
    public boolean appConnect(IConnection con, Object[] params)
    {
        log.debug("new client connectting chat room");       
        return true;
    }
   
    /**
    * 当客户端断开连接的时候调用!
    * 这里我们覆盖了父类的实现。
    */
    public void appDisconnect(IConnection conn)
    {
        log.debug(conn.getClient().getId() + " disconnect");
    }
   
   
    /**
    * 加入聊天室,必须带上用户名,假如用户名为空,则不能发送消息,也不能收到消息。
    * @param params 客户端调用服务器端的参数。
    */
    public void jionChatRoom(Object[] params)
    {
        String nickName = params[0].toString();
        if (null == nickName || "".equals(nickName))
        {
            return ;
        }
        else
        {
            // 设置用户昵称。
            IConnection conn = Red5.getConnectionLocal();
            conn.setAttribute("nickName", nickName);
        }
   
         // 发通知给聊天室的所有人,有新人加入了。
        IScope scope = Red5.getConnectionLocal().getScope();
        Iterator it =   scope.getConnections().iterator();
        int x = 0;
        for (;it.hasNext();)
        {
            Set connections = (Set)it.next();
            IConnection tempConn = (IConnection)connections.iterator().next();
            if (tempConn instanceof IServiceCapableConnection)
            {
                IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
                // 服务器端调用客户端flash方法。
                sc.invoke("showJoinInInfo", new Object[]{nickName});
            }
        }
    }
    /**
    * 给聊天室的所有人发送消息
    * @param params
    */
    public void sayToAll(Object[] params)
    {
        IConnection conn = Red5.getConnectionLocal();
        //conn.setAttribute(”nickName”, nickName);
        String nickName =  conn.getAttribute("nickName").toString();
        String sayWhat = params[0].toString();
        // 发消息给聊天室的所有人.
        IScope scope = Red5.getConnectionLocal().getScope();
        Iterator it = scope.getConnections().iterator();
        for (;it.hasNext();)
        {
            Set connections = (Set)it.next();
            IConnection tempConn = (IConnection)connections.iterator().next();
            if (tempConn instanceof IServiceCapableConnection)
            {
                IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
                // 服务器端调用客户端flash方法。
                sc.invoke("showMessage", new Object[]{nickName+ " : " +sayWhat});
            }
        }
    }
}
 
<?xml version="1.0" encoding="utf-8"?>
<mx:Application fontSize="12" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
 
   private var nc:NetConnection;
 
   private function connectAndJoinRoom(e:Event):void
   {
        var nickName:String = nickNameTextInput.text;
        if (nickName == "")
        {
             return;
        }
        else
        {
             if (nc == null)
             {
              initialNetConnection();
             }
        }
   }
  
   private function initialNetConnection():void
   {
        nc = new NetConnection();
        nc.addEventListener(NetStatusEvent.NET_STATUS, connectStatus);
        nc.client = this;
        nc.connect("rtmp://localhost/echat", null);
   }
  
   private function connectStatus(event:NetStatusEvent) : void
   {
    if (event.info.code == "NetConnection.Connect.Success")
    {
     nc.call("jionChatRoom", null, nickNameTextInput.text);
     sendButton.enabled = true;
    }
   }
  
   private function sendMessage():void
   {
    var sendString:String = inputWhatYouWantSay.text;
    inputWhatYouWantSay.text = "";
    nc.call("sayToAll", null, sendString);
   }
  
   public function showJoinInInfo(message:String) : void
   {
    roomArea.text += message + " 加入聊天室" + "/n";
   }
  
   public function showMessage(message:String) : void
   {
    roomArea.text += message + "/n";
   }
  ]]>
 </mx:Script>
 
 
 <mx:VBox x="20" y="20">
  <mx:HBox>
   <mx:Label text="昵称 : "></mx:Label>
   <mx:TextInput id="nickNameTextInput">
   
   </mx:TextInput>
   <mx:Button click="connectAndJoinRoom(event)" label="加入聊天室">
   
   </mx:Button>
  </mx:HBox>
  <mx:HBox width="100%" height="300">
   <mx:TextArea height="100%" width="100%" id="roomArea">
   
   </mx:TextArea>
  </mx:HBox>
 
  <mx:HBox width="100%">
   <mx:TextInput width="100%" id="inputWhatYouWantSay">
  
   </mx:TextInput>
   <mx:Button enabled="false" id="sendButton" label="发送" click="sendMessage()">
   
   </mx:Button>
  </mx:HBox>
 
 </mx:VBox>
 
</mx:Application>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值