应用创建:
1、新建项目
2、 新建目录WEB-INF,将Red5安装目录下doc\templates\myapp\WEB-INF目录的四个文件复制到WEB-INF中
3、在WEB-INF新建源目录src和编译输出目录classes
下面创建一个空白应用
package com.red5app;
import org.red5.server.adapter.ApplicationAdapter;
/**
* 在Red5的服务器端类体系里,每一个监听的应用实例都需要继承类ApplicationAdapter
* 项目需要引用Red5安装目录下的red5.jar
* @author Administrator
*/
public class Application extends ApplicationAdapter{
/**
* 实现appStart和appStop两个必要的方法
**/
public boolean appStart()
{
return true;
}
public void appStop()
{
}
}
import org.red5.server.adapter.ApplicationAdapter;
/**
* 在Red5的服务器端类体系里,每一个监听的应用实例都需要继承类ApplicationAdapter
* 项目需要引用Red5安装目录下的red5.jar
* @author Administrator
*/
public class Application extends ApplicationAdapter{
/**
* 实现appStart和appStop两个必要的方法
**/
public boolean appStart()
{
return true;
}
public void appStop()
{
}
}
发布应用:
1、编译当前项目代码
2、修改WEB-INF下的四个配置文件
文件log4j.properties是用来指定log4j的工作方式,一般情况下可以不修改;
文件red5-web.properties是用来指定应用程序的目录名和监听的IP地址,根据实际修改
webapp.contextPath
=
/firstapp
webapp.virtualHosts =localhost, 127. 0. 0. 1
webapp.virtualHosts =localhost, 127. 0. 0. 1
文件red5-web.xml是用来指定该应用程序在webapps目录下加载的程序集以及相关的beans
<
?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 = "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 = "com.red5app.Application"
singleton = "true" / >
< ! -- <bean id = "myhandler.service"
class = "the.path.to.my.ServiceHandler"
singleton = "true" / >
-- >
< /beans >
< !DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans >
<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 = "com.red5app.Application"
singleton = "true" / >
< ! -- <bean id = "myhandler.service"
class = "the.path.to.my.ServiceHandler"
singleton = "true" / >
-- >
< /beans >
文件web.xml是用来指定该应用在运行时的Context参数和监听器
<
?xml version
=
"1.0" encoding
=
"ISO-8859-1"
?
>
<web -app
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"
version = "2.4" >
<display -name >firstapp < /display -name >
<context -param >
<param -name >globalScope < /param -name >
<param -value > default < /param -value >
< /context -param >
<context -param >
<param -name >contextConfigLocation < /param -name >
<param -value > /WEB -INF /red5 - *.xml < /param -value >
< /context -param >
<context -param >
<param -name >locatorFactorySelector < /param -name >
<param -value >red5.xml < /param -value >
< /context -param >
<context -param >
<param -name >parentContextKey < /param -name >
<param -value > default.context < /param -value >
< /context -param >
<context -param >
<param -name >log4jConfigLocation < /param -name >
<param -value > /WEB -INF /log4j.properties < /param -value >
< /context -param >
<context -param >
<param -name >webAppRootKey < /param -name >
<param -value > /firstapp < /param -value >
< /context -param >
< ! --由于没有指定监听器,因些需要将listener节点全部删除,避免启动时出错
<listener >
<listener - class >org.springframework.web.util.Log4jConfigListener < /listener - class >
< /listener >
<listener >
<listener - class >org.springframework.web.context.ContextLoaderListener < /listener - class >
< /listener >
-- >
< ! -- remove the following servlet tags if you want to disable remoting for this application -- >
< ! -- 如果不需要对应用远程调试,需要将servlet节点全部删除
<servlet >
<servlet -name >gateway < /servlet -name >
<servlet - class >org.red5.server.net.servlet.AMFGatewayServlet < /servlet - class >
< /servlet >
<servlet -mapping >
<servlet -name >gateway < /servlet -name >
<url -pattern > /gateway < /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>
<web -app
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"
version = "2.4" >
<display -name >firstapp < /display -name >
<context -param >
<param -name >globalScope < /param -name >
<param -value > default < /param -value >
< /context -param >
<context -param >
<param -name >contextConfigLocation < /param -name >
<param -value > /WEB -INF /red5 - *.xml < /param -value >
< /context -param >
<context -param >
<param -name >locatorFactorySelector < /param -name >
<param -value >red5.xml < /param -value >
< /context -param >
<context -param >
<param -name >parentContextKey < /param -name >
<param -value > default.context < /param -value >
< /context -param >
<context -param >
<param -name >log4jConfigLocation < /param -name >
<param -value > /WEB -INF /log4j.properties < /param -value >
< /context -param >
<context -param >
<param -name >webAppRootKey < /param -name >
<param -value > /firstapp < /param -value >
< /context -param >
< ! --由于没有指定监听器,因些需要将listener节点全部删除,避免启动时出错
<listener >
<listener - class >org.springframework.web.util.Log4jConfigListener < /listener - class >
< /listener >
<listener >
<listener - class >org.springframework.web.context.ContextLoaderListener < /listener - class >
< /listener >
-- >
< ! -- remove the following servlet tags if you want to disable remoting for this application -- >
< ! -- 如果不需要对应用远程调试,需要将servlet节点全部删除
<servlet >
<servlet -name >gateway < /servlet -name >
<servlet - class >org.red5.server.net.servlet.AMFGatewayServlet < /servlet - class >
< /servlet >
<servlet -mapping >
<servlet -name >gateway < /servlet -name >
<url -pattern > /gateway < /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>
3、在Red5安装目录的webapp目录下创建目录firstapp,在firstapp目录下创建WEB-INF,然后将工程目录下的WEB-INF里的classes文件夹和4个配置文件全部复制到firstapp下的WEB-INF里
4、重启Red5服务
查看Red5安装目录log下的red5_services.log文件,当出现如下提示说明应用成功被Red5服务器加载:
INFO | jvm 1 | 2014/09/14 22:02:34 | [INFO] [Launcher:/firstapp] org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in
客户端连接:(以AS3为例)
AS3与Red5服务器进行通信主要有:NetConnection、NetStream、SharedObject。调用顺序如下图所示:
INFO | jvm 1 | 2014/09/14 22:02:34 | [INFO] [Launcher:/firstapp] org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in
客户端连接:(以AS3为例)
AS3与Red5服务器进行通信主要有:NetConnection、NetStream、SharedObject。调用顺序如下图所示:
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.SharedObject;
var nc:NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
var so:SharedObject;
function netStatus(event:NetStatusEvent):void
{
trace("event.info.code:"+event.info.code);
switch(event.info.code)
{
case "NetConnection.Connect.Success":
var so:SharedObject = SharedObject.getRemote("UserSO",nc.uri,true);
break;
}
}
nc.connect("rtmp://127.0.0.1/firstapp");//连接时地址不需要端口号