前段时间一直想研究Red5 但是发现网上的很多例子 和配置的方法都跑不通
不知道是我Red5版本问题 还是JDK编译问题
先说下我的环境 JDK1.6 MyEclipse7.5 Red5 0.9.0 windows安装版
安装没什么特别好说的 如果你想设置成服务 就勾上 影响都不大 我一般是手动启动Red5的
先从最基本的 连接开始
先写Java 端 MyEclipse 新建一个Web工程 Path 加入Red5 lib 文件夹下的jar 不过除了 servlet *.jar 和 jsp* .jar 别忘了外面的 red5.jar
新建一个Java 类 一般命名为 Application 让它继承 ApplicationAdapter Java代码如下
public class Application extends ApplicationAdapter { private Logger log = Red5LoggerFactory.getLogger(Application.class,"myDemo"); private IScope appScope; private IServerStream serverStream; public Application(){ log.info("myDemo created"); System.out.println("myDemo created"); } public boolean appStart(IScope app) { log.info("myDemo appStart"); System.out.println("myDemo appStart"); this.appScope = app; return true; } public boolean appConnect(IConnection conn, Object[] params) { log.info("myDemo appConnect"); System.out.println("connect"); return true; } public void appDisconnect(IConnection conn) { log.info("myDemo appDisconnect"); if((this.appScope == conn.getScope()) && (this.serverStream != null)){ this.serverStream.close(); } super.appDisconnect(conn); } }
无非是连接上 打印一些日志 和控制台打印一些字符串
接着是Flex 端
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="Red5 Connect Test" creationComplete="appinit()"> <mx:Script> <![CDATA[ import mx.controls.Alert; import flash.display.Sprite; import flash.events.NetStatusEvent; import flash.events.SecurityErrorEvent; import flash.media.Video; import flash.net.NetConnection; import flash.net.NetStream; import flash.events.Event; var command:String="rtmp://localhost/myDemo";//这里注意修改地址为你的red5服务器地址 var mync:NetConnection=new NetConnection(); var client1:Object = new Object(); function netStatusHandler(sevt:NetStatusEvent):void{ } function appinit(){ mync.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler); } function serverConnect():void{ out_txt.text="Connectting ... ..."; client1.clientMethod = this.clientMethod; mync.client = this.client1; mync.connect(command); trace("serverConnect:"+command); } ]]> </mx:Script> <mx:Style> WindowedApplication { background-color:"0xffffff"; background-alpha:"0.5"; } </mx:Style> <mx:Label y="100" text="连接状态:未连接" horizontalCenter="0" fontSize="12" fontWeight="normal" id="out_txt" condenseWhite="true" height="150"/> <mx:Button y="254" label="点击我连接Red5" fontSize="11" fontWeight="normal" horizontalCenter="0" click="serverConnect()"/> </mx:WindowedApplication>
再来看最关键的配置文件
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>myDemo</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>/myDemo</param-value> </context-param> <listener> <listener-class>org.red5.logging.ContextLoggingListener</listener-class> </listener> <filter> <filter-name>LoggerContextFilter</filter-name> <filter-class>org.red5.logging.LoggerContextFilter</filter-class> </filter> <filter-mapping> <filter-name>LoggerContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>rtmpt</servlet-name> <servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rtmpt</servlet-name> <url-pattern>/fcs/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>rtmpt</servlet-name> <url-pattern>/open/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>rtmpt</servlet-name> <url-pattern>/close/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>rtmpt</servlet-name> <url-pattern>/send/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>rtmpt</servlet-name> <url-pattern>/idle/*</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>Forbidden</web-resource-name> <url-pattern>/streams/*</url-pattern> </web-resource-collection> <auth-constraint/> </security-constraint> </web-app>
red5-web.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd"> <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/red5-web.properties" /> </bean> <bean id="web.context" class="org.red5.server.Context" autowire="byType" /> <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" /> <property name="handler" ref="web.handler" /> <property name="contextPath" value="${webapp.contextPath}" /> <property name="virtualHosts" value="${webapp.virtualHosts}" /> </bean> <bean id="web.handler" class="org.red5.demos.oflaDemo.Application" /> <bean id="demoService.service" class="org.red5.demos.oflaDemo.DemoService" /> <bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/> </beans>
red5-web.properties
webapp.contextPath=/myDemo webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088
将工程发布到Red5的 webapps 文件夹下 命名为你定义的名字 这里我的是 myDemo
到这里 已经完成最简单的Demo 了