1. 程式人生 > >將red5整合到Tomcat伺服器中

將red5整合到Tomcat伺服器中

2.將ROOT.war解壓。將其目錄下web-info/lib目錄下的jar全部拷貝到myApp對應的lib中。拷貝red5安裝目錄的red5.jar到myApp/WEB-INFO/lib目錄下。這樣spring框架與red5需要的包都匯入了

3.將ROOT.war解壓後的資料夾中WEB-INFO/classes/目錄下的除echo-web.xml,admin-web.xml,SOSample-web.xml,oflaDemo-web.xml,web.xml五個無檔案外的其他檔案拷貝到myApp的src下。並修改所有檔案中的埠5080都改為8080。我安裝tomcat時選擇的9090所以 對應位置改為9090。

4.在src中新建一個自己專案的myApp-web.xml內容照著沒要的檔案改,如對應oflaDemo-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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd

http://www.springframework.org/schema/langhttp://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

    <!-- SOSample -->
    <bean id="myApp.context" class="org.red5.server.Context" autowire="byType" />
   
    <bean id="myApp.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="myApp.context" />
        <property name="handler" ref="myApp.handler" />
        <property name="contextPath" value="/myApp" />
        <property name="virtualHosts"
            value="*,localhost, localhost:9090, 127.0.0.1:9090" />         
    </bean>

    <bean id="myApp.handler" class="red5.example.red5server.Application" />


</beans>

紅色部分為自己的應用伺服器類。

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

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/classes/*-web.xml</param-value>
</context-param>

<listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<listener>
   <listener-class>org.red5.server.war.WarLoaderServlet</listener-class>
</listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

<servlet>
   <servlet-name>gateway</servlet-name>
   <servlet-class>org.red5.server.net.servlet.AMFGatewayServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
   <servlet-name>rtmpt</servlet-name>
   <servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
   <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>gateway</servlet-name>
   <url-pattern>/gateway</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>/idle/*</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>/close/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
   <welcome-file>login.html</welcome-file>
   <welcome-file>index.html</welcome-file>
   <welcome-file>index.htm</welcome-file>
</welcome-file-list>

<security-constraint>
   <web-resource-collection>
    <web-resource-name>Forbidden</web-resource-name>
    <url-pattern>/WEB-INF/*</url-pattern>
   </web-resource-collection>
   <auth-constraint />
</security-constraint>

<security-constraint>
   <web-resource-collection>
    <web-resource-name>Forbidden</web-resource-name>
    <url-pattern>/persistence/*</url-pattern>
   </web-resource-collection>
   <auth-constraint />
</security-constraint>

<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>




6.在src中新建包red5.example.red5server。在包中新建類Application:內容如下

package red5.example.red5server;

import org.red5.server.adapter.ApplicationAdapter;


public class Application extends ApplicationAdapter {

public String login(){
   return "Welcome to Chat Servers";
}

}


然後部署到tomcat。啟動不報錯則成功。

接下來編寫flex客戶端。

程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml
" layout="absolute" fontFamily="Arial">
<mx:Script>
   <![CDATA[
    public function connect():void
    {
     var nc:NetConnection = new NetConnection();
     trace("連結伺服器......");
     nc.connect("rtmp://localhost/myApp");
     nc.call("login",new Responder(_onGet,_onError));
    }
   
    private function _onError(obj:Object):void {
       text1.text =""+ obj;
    }
   
    private function _onGet(obj:Object):void {
       text1.text =""+ obj;
      }
   ]]>
</mx:Script>
<mx:Button x="118" y="216" label="按鈕" fontFamily="Verdana" fontSize="12" click="connect()" id="button1"/>
<mx:Label x="119" y="149" text="Label" width="245" color="#F8F2F8" height="23" fontSize="15" id="text1"/>
</mx:Application>

即可測試 如圖:

有時啟動,執行連結失敗。 發現是我的tomcat中webapps資料夾下的多個red5專案造成的。

測試的時候最好將其他的red5相關專案移除。也不知道具體是怎麼回事!