Red5学习笔记(2)

研究完red5的demo之后,我们可以手动写个小例子,试试连接red5。

        准备步骤,打开MyEclipse和Flex Builder。MyEclipse做一个web工程,写一个简单的Java文件

 

 

Java代码 复制代码
  1. package org.red5.test;   
  2.   
  3. import org.red5.server.adapter.ApplicationAdapter;   
  4. import org.red5.server.api.IClient;   
  5. import org.red5.server.api.IConnection;   
  6. import org.red5.server.api.IScope;   
  7.   
  8. public class Application extends ApplicationAdapter {   
  9.     private IScope app = null;   
  10.   
  11.     public boolean appStart(IScope __app) {   
  12.         app = __app;   
  13.         super.appStart(app);   
  14.         return true;   
  15.     }   
  16.   
  17.     @Override  
  18.     public synchronized boolean connect(IConnection conn, IScope scope, Object[] params) {   
  19.         System.out.println("connect");   
  20.         IClient client = conn.getClient();   
  21.         String uid = client.getId();   
  22.         System.out.println("connect->uid:" + uid);   
  23.         System.out.println("welcome to red5 world !");   
  24.         return true;   
  25.     }   
  26.   
  27. }  
package org.red5.test;

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

public class Application extends ApplicationAdapter {
	private IScope app = null;

	public boolean appStart(IScope __app) {
		app = __app;
		super.appStart(app);
		return true;
	}

	@Override
	public synchronized boolean connect(IConnection conn, IScope scope, Object[] params) {
		System.out.println("connect");
		IClient client = conn.getClient();
		String uid = client.getId();
		System.out.println("connect->uid:" + uid);
		System.out.println("welcome to red5 world !");
		return true;
	}

}

     然后将整个WebRoot 拷贝到red5目录下面的webapps,然后在WEB-INF目录下补充上 red5-web.xml 和 red5-web.properties 文件,将red5-web.xml 里面的修改为

 

 

Xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
  3. <beans>  
  4.        
  5.     <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  6.         <property name="location" value="/WEB-INF/red5-web.properties" />  
  7.     </bean>  
  8.        
  9.     <bean id="web.context" class="org.red5.server.Context"    
  10.         autowire="byType" />  
  11.        
  12.     <bean id="web.scope" class="org.red5.server.WebScope"  
  13.          init-method="register">  
  14.         <property name="server" ref="red5.server" />  
  15.         <property name="parent" ref="global.scope" />  
  16.         <property name="context" ref="web.context" />  
  17.         <property name="handler" ref="web.handler" />  
  18.         <property name="contextPath" value="${webapp.contextPath}" />  
  19.         <property name="virtualHosts" value="${webapp.virtualHosts}" />  
  20.     </bean>  
  21.   
  22.     <bean id="web.handler"    
  23.         class="org.red5.test.Application"    
  24.         singleton="true" />  
  25.   
  26. </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="org.red5.test.Application" 
		singleton="true" />

</beans>

 

然后修改web.xml文件:

<?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>Red5App</display-name>
 
 <context-param>
  <param-name>Red5App</param-name>
  <param-value>/red5_app2</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>
     <display-name>SecurityTest</display-name>
     <servlet-name>SecurityTest</servlet-name>
     <servlet-class>org.red5.demos.oflaDemo.SecurityTest</servlet-class>
    </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>
 <!--
 <servlet-mapping>
  <servlet-name>SecurityTest</servlet-name>
  <url-pattern>/securitytest</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,没有错误可以说明部署成功了。

 

开发一个客户端去连接red5,可以使用Flex 工具编写,不管你是做Web application 还是 Desktop application其实都一样。代码:

 

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  3.     <mx:Script>  
  4.        <![CDATA[  
  5.        import flash.display.Sprite;  
  6.         import flash.events.NetStatusEvent;  
  7.         import flash.events.SecurityErrorEvent;  
  8.         import flash.media.Video;  
  9.         import flash.net.NetConnection;  
  10.         import flash.net.NetStream;  
  11.         import flash.events.Event;  
  12.       
  13.         
  14.        var command:String="rtmp://192.168.61.115:1935/red5_app";//这里注意修改地址为你的red5服务器地址  
  15.        var mync:NetConnection=new NetConnection();  
  16.         
  17.        function netStatusHandler(sevt:NetStatusEvent):void{  
  18.         var str="_____________________netStatusHandler_____________________\n";     
  19.         str+="code:"+sevt.info.code+"\n";  
  20.         str+="level:"+sevt.info.level+"\n";  
  21.          
  22.         if(sevt.info.application!=undefined){  
  23.          str+="application:"+sevt.info.application+"\n";  
  24.         }  
  25.          
  26.         if(sevt.info.description!=undefined){  
  27.          str+="description:"+sevt.info.description+"\n";  
  28.          out_txt.text=str;  
  29.         }      
  30.        }  
  31.       
  32.         
  33.        function appinit(){  
  34.           
  35.         mync.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);  
  36.  
  37.        }  
  38.         
  39.        function serverConnect():void{  
  40.         appinit();  
  41.         out_txt.text="Connectting ... ...";  
  42.         mync.connect(command,true);  
  43.         trace("serverConnect:"+command);  
  44.        }  
  45.         
  46.        ]]>  
  47.     </mx:Script>  
  48.   
  49.     <mx:Style>  
  50.        WindowedApplication   
  51.        {   
  52.         background-color:"0xffffff";   
  53.         background-alpha:"0.5";   
  54.        }   
  55.     </mx:Style>  
  56.   
  57.     <mx:Label y="100" text="连接状态:未连接" horizontalCenter="0" fontSize="12" fontWeight="normal" id="out_txt" condenseWhite="true" height="150"/>  
  58.     <mx:Button y="254" label="点击我连接Red5" fontSize="11" fontWeight="normal" horizontalCenter="0" click="serverConnect()"/>  
  59. </mx:Application>  

你可能感兴趣的:(spring,bean,Web,servlet,Flash)