服务器端:
red5的项目的建立与tomact非常的相似.
1、在 Red5的安装路径C:\Program Files\Red5\webapps下建立自己的项目文件夹:
2、将C:\Program Files\Red5\doc\templates\myapp下的文件夹WEB-INF复制到自己的项目中,WEB-INF里是red5主要的项目配置文件。
3、 在WEB-INF下建立classes以及src文件夹。
4、在项目根目录下建立Eclipes项目。classes为编译目录,src为源码目录。
5、导入red5 jar包。
6、修改配置文件:
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>My sample Red5 application</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>
<!--如果不注释需要导入log4j的jar包。。。。。。。一定小心
<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>/chatApp</param-value>
</context-param>
<!--
<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-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>My sample Red5 application</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>
<!--如果不注释需要导入log4j的jar包。。。。。。。一定小心
<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>/chatApp</param-value>
</context-param>
<!--
<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-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>
red5-web.xml
<?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.papaya.Application"
singleton="true" />
<!-- this will support calling methods through "myhandler.<methodName>" -->
<!--
<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.papaya.Application"
singleton="true" />
<!-- this will support calling methods through "myhandler.<methodName>" -->
<!--
<bean id="myhandler.service"
class="the.path.to.my.ServiceHandler"
singleton="true" />
-->
</beans>
red5-web.properties
webapp.contextPath=/chatApp
webapp.virtualHosts=localhost, 127.0.0.1
webapp.virtualHosts=localhost, 127.0.0.1
7、建立程序的入口类
Application.java
package com.papaya;
import org.red5.io.utils.ObjectMap;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IScope;
import com.papaya.VO.MapConventPojo;
import com.papaya.VO.User;
public class Application extends ApplicationAdapter
{
public String sayHello(String name)
{
System.out.println("hello"+name);
return "hello"+name;
}
public boolean appStart(IScope scope)
{
System.out.println("聊天服务器启动了!!!!");
return true;
}
public Object sendVo(ObjectMap message)
{
User user =new User();
MapConventPojo.Convent(message, user);
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getSchool());
return user;
}
}
import org.red5.io.utils.ObjectMap;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IScope;
import com.papaya.VO.MapConventPojo;
import com.papaya.VO.User;
public class Application extends ApplicationAdapter
{
public String sayHello(String name)
{
System.out.println("hello"+name);
return "hello"+name;
}
public boolean appStart(IScope scope)
{
System.out.println("聊天服务器启动了!!!!");
return true;
}
public Object sendVo(ObjectMap message)
{
User user =new User();
MapConventPojo.Convent(message, user);
System.out.println(user.getAge());
System.out.println(user.getName());
System.out.println(user.getSchool());
return user;
}
}
8、建立客户端flash类
package com.renaun.samples.net
{
import com.game.Logger;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetConnection;
import com.game.Logger;
/**
* Note: This class was dynamic in ActionScript 2.0 but is now sealed.
* To write callback methods for this class, you can either extend the
* class and define the callback methods in your subclass, or you can
* use the client property to refer to an object and define the callback
* methods on that object.
*/
dynamic public class FMSConnection extends NetConnection implements IEventDispatcher
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public static const SUSSESS:String="success";
public static const FAILURE:String="Failure";
/**
* Constructor
*/
public function FMSConnection()
{
super();
}
public var clientID:Number;
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Connect
*/
override public function connect( url:String, args ):void
{
// Set object encoding to be compatible with Flash Media Server
this.objectEncoding = flash.net.ObjectEncoding.AMF0;
NetConnection.defaultObjectEncoding
// Add status/security listeners
this.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
this.addEventListener( SecurityErrorEvent.SECURITY_ERROR, netSecurityError );
this.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
this.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
// TODO does not pass args into the super function
super.connect( url );
}
/**
* setID
*/
public function setId( id:Number ):*
{
Logger.debug( "FMSConnection::setId: id=" + id );
if( isNaN( id ) ) return;
clientID = id;
return "Okay";
}
/**
* Status Handler for the NetConnection class
*/
private function netStatusHandler( event:NetStatusEvent ):void
{
switch( event.info.code ) {
case "NetConnection.Connect.Success":
Logger.debug( "FMSConnection:netStatusHandler:Success: connected: " + this.connected );
dispatchEvent( new Event(FMSConnection.SUSSESS) );
break;
case "NetConnection.Connect.Failed":
Logger.debug( "FMSConnection:netStatusHandler:Failed: connected: " + this.connected + " - " + event.info.code );
dispatchEvent( new Event(FMSConnection.FAILURE) );
break;
default:
Logger.debug( "FMSConnection:netStatusHandler:code: " + event.info.code );
break;
}
}
private function netSecurityError( event:SecurityErrorEvent ):void {
Logger.error( "FMSConnection:netSecurityError: " + event );
}
private function asyncErrorHandler( event:AsyncErrorEvent ):void {
Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.error );
}
private function ioErrorHandler( event:IOErrorEvent ):void {
Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.text );
}
}
}
{
import com.game.Logger;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.net.NetConnection;
import com.game.Logger;
/**
* Note: This class was dynamic in ActionScript 2.0 but is now sealed.
* To write callback methods for this class, you can either extend the
* class and define the callback methods in your subclass, or you can
* use the client property to refer to an object and define the callback
* methods on that object.
*/
dynamic public class FMSConnection extends NetConnection implements IEventDispatcher
{
//--------------------------------------------------------------------------
//
// Constructor
//
//--------------------------------------------------------------------------
public static const SUSSESS:String="success";
public static const FAILURE:String="Failure";
/**
* Constructor
*/
public function FMSConnection()
{
super();
}
public var clientID:Number;
//--------------------------------------------------------------------------
//
// Methods
//
//--------------------------------------------------------------------------
/**
* Connect
*/
override public function connect( url:String, args ):void
{
// Set object encoding to be compatible with Flash Media Server
this.objectEncoding = flash.net.ObjectEncoding.AMF0;
NetConnection.defaultObjectEncoding
// Add status/security listeners
this.addEventListener( NetStatusEvent.NET_STATUS, netStatusHandler );
this.addEventListener( SecurityErrorEvent.SECURITY_ERROR, netSecurityError );
this.addEventListener( AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler );
this.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );
// TODO does not pass args into the super function
super.connect( url );
}
/**
* setID
*/
public function setId( id:Number ):*
{
Logger.debug( "FMSConnection::setId: id=" + id );
if( isNaN( id ) ) return;
clientID = id;
return "Okay";
}
/**
* Status Handler for the NetConnection class
*/
private function netStatusHandler( event:NetStatusEvent ):void
{
switch( event.info.code ) {
case "NetConnection.Connect.Success":
Logger.debug( "FMSConnection:netStatusHandler:Success: connected: " + this.connected );
dispatchEvent( new Event(FMSConnection.SUSSESS) );
break;
case "NetConnection.Connect.Failed":
Logger.debug( "FMSConnection:netStatusHandler:Failed: connected: " + this.connected + " - " + event.info.code );
dispatchEvent( new Event(FMSConnection.FAILURE) );
break;
default:
Logger.debug( "FMSConnection:netStatusHandler:code: " + event.info.code );
break;
}
}
private function netSecurityError( event:SecurityErrorEvent ):void {
Logger.error( "FMSConnection:netSecurityError: " + event );
}
private function asyncErrorHandler( event:AsyncErrorEvent ):void {
Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.error );
}
private function ioErrorHandler( event:IOErrorEvent ):void {
Logger.error( "FMSConnection:asyncErrorHandler: " + event.type + " - " + event.text );
}
}
}