red5 與tomcat集成配置 簡單入門實例

這裏不再介紹什麼是red5了,直接寫配置。

我用的是red5 0.8版,首先從網上下載兩個文件:red5-war-0.8.0.zip 和 setup-Red5-0.8.0.exe,將red5-war-0.8.0.zip 解壓,得到root.war,

1.將root.war複製到tomcat安裝目錄下的webapps,解壓到root文件夾,替換原來的root文件夾,啓動tomcat,得到red5的測試頁面,red5集成到tomcat完成。


2.安裝 setup-Red5-0.8.0.exe.

3.下面來編寫red5的測試項目。

4.新建Web項目HelloRed5(我是將集成了red5的tomcat配到Myeclipse,在Myeclipse編寫項目), setup-Red5-0.8.0.exe的安裝路徑下找到red5.jar,boot.jar,複製到項目的lib下,再到tomcat的webapps\ROOT\WEB-INF\lib路徑下複製所有的jar包到項目的lib下,到tomcat的webapps\ROOT\WEB-INF\classes目錄下,將裏面所有的配置文件複製到項目src下,將tomcat的webapps\ROOT\WEB-INF目錄下複製web.xml到項目下,覆蓋原來的web.xml.

此時,我們項目目錄爲:

5.然後我們編寫後臺的測試類:

package red5.example.red5server;

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

public class Application extends ApplicationAdapter {
	public boolean appConnect(IConnection conn, Object[] args) {
		System.out.println(" 連接");
		return true;
	}

	public String change(String str) {
		System.out.println(" 客戶端調用服務器");
		return str.toUpperCase();// 傳入的字符串轉換成大寫後返回
	}
}

5.下面我們來修改配置,使項目能成爲一個red5的項目發佈。

   首先,將web.xml中的

<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>/</param-value>
	</context-param>

修改爲

	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>/HelloRed5</param-value> <!-- HelloRed5 爲項目名  -->
	</context-param>
6.在src目錄下新建一個HelloRed5-web.xml(HelloRed5爲項目名),內容爲

<?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.red5Server" 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.red5Server" />  
        <property name="handler" ref="web.handler.red5Server" />  
        <property name="contextPath" value="/HelloRed5" />   <!-- HelloRed5爲項目名 -->
       <property name="virtualHosts"  
            value="*,localhost, localhost:8080, 127.0.0.1:8080" />  
    </bean>  
  
    <bean id="web.handler.red5Server"  
       class="red5.example.red5server.Application"/>  <!-- 此處配置的就是前面編寫的測試類 -->
</beans>  

然後到root-web.xml中刪除以下內容:

	<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="global.handler" />
		<property name="contextPath" value="/" />
		<property name="virtualHosts" value="*,localhost, localhost:8080, 127.0.0.1:8080" />
	</bean>
因爲這個bean對象已經在HelloRed5-web.xml中定義好了。

7.到re5-core.xml中找到

	<!-- RTMP Mina Transport -->
	<bean id="rtmpTransport" class="org.red5.server.net.rtmp.RTMPMinaTransport" init-method="start" destroy-method="stop">
		<property name="ioHandler" ref="rtmpMinaIoHandler" />
        <property name="connectors">
            <list>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  
                    <constructor-arg index="1" type="int" value="1935" />  
                </bean>
                <!-- You can now add additional ports and ip addresses
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  
                    <constructor-arg index="1" type="int" value="1936" />  
                </bean>
                 -->
            </list>
        </property>		
		<property name="receiveBufferSize" value="65536" />
		<property name="sendBufferSize" value="271360" />
		<property name="eventThreadsCore" value="4" />
		<property name="eventThreadsMax" value="8" />
		<property name="eventThreadsQueue" value="-1 " />
		<property name="eventThreadsKeepalive" value="60" />
		<!-- This is the interval at which the sessions are polled for stats. If mina monitoring is not
				enabled, polling will not occur. -->
		<property name="jmxPollInterval" value="1000" />
		<property name="tcpNoDelay" value="true" />
	</bean>

將其中<constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  中的value改爲你電腦的IP地址,將其中 <constructor-arg index="1" type="int" value="1935" /> 中的value改爲8080,如我的IP地址爲10.21.2.127,則我將其修改爲:

	<!-- RTMP Mina Transport -->
	<bean id="rtmpTransport" class="org.red5.server.net.rtmp.RTMPMinaTransport" init-method="start" destroy-method="stop">
		<property name="ioHandler" ref="rtmpMinaIoHandler" />
        <property name="connectors">
            <list>
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="10.21.2.127" />  
                    <constructor-arg index="1" type="int" value="8080" />  
                </bean>
                <!-- You can now add additional ports and ip addresses
                <bean class="java.net.InetSocketAddress">
                    <constructor-arg index="0" type="java.lang.String" value="0.0.0.0" />  
                    <constructor-arg index="1" type="int" value="1936" />  
                </bean>
                 -->
            </list>
        </property>		
		<property name="receiveBufferSize" value="65536" />
		<property name="sendBufferSize" value="271360" />
		<property name="eventThreadsCore" value="4" />
		<property name="eventThreadsMax" value="8" />
		<property name="eventThreadsQueue" value="-1 " />
		<property name="eventThreadsKeepalive" value="60" />
		<!-- This is the interval at which the sessions are polled for stats. If mina monitoring is not
				enabled, polling will not occur. -->
		<property name="jmxPollInterval" value="1000" />
		<property name="tcpNoDelay" value="true" />
	</bean>

8.將項目發佈到tomcat中,如果啓動成功,看到如下提示,則快成功了



9.接着我們編寫客戶端,新建一個flex項目,內容爲:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
				fontSize="12" backgroundGradientAlphas="[1.0, 1.0]"
				backgroundGradientColors="[#FDF9F9, #FDF9F9]" width="442" height="344" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			private var rtmpURL:String="rtmp://10.21.2.127:8080/HelloRed5";
			private var conn:NetConnection=new NetConnection();
			private var isConnectSuccess:Boolean=false;

			
			/**
			 * 初始化時,連接到red5
			 */
			private function init():void{
				conn.addEventListener(NetStatusEvent.NET_STATUS,netStatus); //netStatus爲回調函數
				conn.connect(rtmpURL);
			}
			
			private function netStatus(e:NetStatusEvent):void{
				Alert.show(e.info.code);
				if(e.info.code=="NetConnection.Connect.Success"){
					isConnectSuccess=true;
				}
			}
			
			private function click(e:MouseEvent):void{
				if(!isConnectSuccess){
					Alert.show("還沒連接到服務器");
				}
				else{
					conn.call("change",new Responder(resultFun),str.text);//change 是服務器端方法名稱,Responder包括對應的處理函數
				}
			}
			
			
			private function resultFun(object:String):void{
				trace(object);
				result.text=object.toString();
			}
			
		]]>
	</mx:Script>
	
	<mx:Label x="129" y="119" text="顯示從服務器端返回的字符" id="result" width="160" fontSize="12"/>
	<mx:TextInput x="129" y="145" id="str" width="212"/>
	<mx:Button x="224" y="175" label="調用服務器方法" click="this.click(event)"/>
	
	
</mx:Application>

運行後看到以下效果,則成功了:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章