將red5集成到Tomcat服務器中

將red5集成到Tomcat服務器中
2009-07-03 17:00

http://dl.fancycode.com/red5/0.6.3/war/ 下載的是java5目錄下的三個war(ROOT.waradmin.warecho.war)中的ROOT.war) 並下載安裝 win:setup-red5-0.6.3.exe

1.在Eclipse下新建一個web應用,起名叫myApp。

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

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

紅色部分爲自己的應用服務器類。

7.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
   version="2.4"
   xmlns="
http://java.sun.com/xml/ns/j2ee"
   xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
   xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">
<!--
    ** For use with servlet v2.5 replace the lines above with these
    version="2.5"
    xmlns="
http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
-->
<context-param>
   <param-name>globalScope</param-name>
   <param-value>default</param-value>
</context-param>

<context-param>
   <param-name>parentContextKey</param-name>
   <param-value>default.context</param-value>
</context-param>

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

將編譯後的flex工程下bin-debug文件夾下的文件全部複製到tomcat/webapps/myApp中。
訪問:http://localhost:9090/myApp/red5Client.html
http://localhost:9090/myApp/red5Client.swf

即可測試 如圖:

 

有時啓動,運行鏈接失敗。 發現是我的tomcat中webapps文件夾下的多個red5項目造成的。

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

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