现在的位置: 首页 > 综合 > 正文

小试 Red5 流媒体服务器

2013年10月21日 ⁄ 综合 ⁄ 共 5961字 ⁄ 字号 评论关闭

原文链接:http://www.oschina.net/question/12_7991

 

1. 什么是Red5.
Red5是一个开源项目,用于实现flash与服务器端之间通过rtmp(real time messaging protocal)协议通信,可以实现视频、音频的传输,remote shared object等等。相对于FMS, Red5是免费、开源的。

2. 环境搭建(用Tomcat好了).
首先下载Red5,我们这里适用0.7版。下载地址:
http://osflash.org/red5/070final, 选择相应的版本就可以了。下载安装包比较好,里面资料比较多,有api文档,demo源码等等,推荐下载下来看一下。
搭建Red5的环境非常简单,只要把用到的jar包放到lib目录下面,再配置一下相应的xml文件即可。
在Red5的安装目录下面,有我们要用到的所有东西,目录结构如下:
 

在Eclipse里面新建一个Dynamic Web Project, 命名为Red5ChatRoom, 直接finish.
把Red5安装目录下的red5.jar和lib目录下的jar包复制一份到Red5ChatRoom项目的WEB-INF/lib下。

接着,修改red5配置文件。
第一个要修改的是web.xml文件,我们可以直接从red5安装目录下面的conf目录下复制一份。
第二个就是把Red5必须要的配置文件放到class path中,这些配置文件是容器一启动的时候,Red5自动去读取的。主要是下面这些配置文件beanRefContext.xml, defaultContext.xml, red5-common.xml, red5-core.xml, 这四个文件是必须的。其中beanRefContext.xml中说明了要加载其他三个文件。
所以,简单的方式就是在WEB-INF目录下面新建一个classes目录,把上面4个文件复制到classes中。
这样,Red5的基本环境就配置完了。

 

3. 写个简单一点的Demo,实时聊天室.(由于是demo,所以没有检测之类的功能,功能点就是有用户名、能群聊天)
首先实现server端:
package demo;

import java.util.Iterator;
import java.util.Map;

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;

public class Red5RealTimeChatR extends ApplicationAdapter {

 /**
  * 每个新的客户端来连接的时候调用!
  * 这里我们覆盖了父类的实现。
  */
 public boolean appConnect(IConnection con, Object[] params) {
  System.out.println(”new client connectting chat room”);
  return true;
 }
 
 /**
  * 当客户端断开连接的时候调用!
  * 这里我们覆盖了父类的实现。
  */
 public void appDisconnect(IConnection conn) {
  System.out.println(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();
  int x = 0;
  for (;it.hasNext();) {
   IConnection tempConn = (IConnection)it.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();
  for (;it.hasNext();) {
   IConnection tempConn = (IConnection)it.next();
   if (tempConn instanceof IServiceCapableConnection) {
    IServiceCapableConnection sc = (IServiceCapableConnection) tempConn;
    // 服务器端调用客户端flash方法。
    sc.invoke(”showMessage”, new Object[]{nickName+” : “+sayWhat});
   }
  }
 }
}
这个类继承自ApplicationAdapter, 所以当有rtmp请求时,它可以作为处理类。
写完这个后,得配置到Red5ChatRoom项目里面,用最简单的方法,在class path下面新建一个xml文件,比如Red5ChatRoom-web.xml(Red5默认会加载class path下面的*-web.xml)。
内容如下:
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “
http://www.springframework.org/dtd/spring-beans.dtd“>
<beans>
 <bean id=”web.context.chatroom” class=”org.red5.server.Context”>
  <property name=”scopeResolver” ref=”red5.scopeResolver” />
  <property name=”clientRegistry” ref=”global.clientRegistry” />
  <property name=”serviceInvoker” ref=”global.serviceInvoker” />
  <property name=”mappingStrategy” ref=”global.mappingStrategy” />
 </bean>
 <bean id=”web.scope” class=”org.red5.server.WebScope” init-method=”register”>
  <property name=”server” ref=”red5.server” />
  <property name=”parent” ref=”global.scope” />
  <property name=”context” ref=”web.context.chatroom” />
  <property name=”handler” ref=”web.handler” />
  <property name=”contextPath” value=”/Red5ChatRoom” />
  <property name=”virtualHosts” value=”*,localhost,localhost:8080,127.0.0.1:8080″ />
 </bean>
 <bean id=”web.handler.chatroom” class=”demo.Red5RealTimeChatR” />
</beans>
这是最简单的Red5项目应用配置。
现在就可以发布到Tomcat下面去了。启动以后,我们得写Flash客户端来联调。

首先,接下来我们就新建一个Flex Project,项目名称就叫ChatRoom吧。
做一个简单的聊天室界面。
图片一张。
代码如下:
<?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://127.0.0.1/Red5ChatRoom", 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>

运行Flex应用,在昵称里面填入内容,加入聊天室以后,就可以和在线的人一起聊天了。

上面是一个非常非常简单的版本,修改一下就可以实现显示在线人员,给特定人员发消息等。

抱歉!评论已关闭.