Red5研究与积累(三)

上一节我们做了一个客户端调用服务端的例子  这一节我们来换换 服务端调用客户端的例子

 

配置文件

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	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">
  <display-name>red5Demo2</display-name>
	
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value>/red5Demo2</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-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>
    
    <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"?>
<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">

	<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.demos.Application" />

	<bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor"/>


</beans>

 

 

red5-web.properties

 

webapp.contextPath=/red5Demo2
webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088

 

 

接着是Java 服务端

 

public class Application extends ApplicationAdapter implements IPendingServiceCallback {
	
	private IScope appScope;
	
	public Application(){
		log.info("myDemo created");
		System.out.println("myDemo created");
	}

	public boolean appStart(IScope app) {
		log.info("myDemo appStart");
		System.out.println("myDemo appStart");
		this.appScope = app;
		return true;
	}

	public boolean appConnect(IConnection conn, Object[] params) {
		log.info("myDemo appConnect");
		System.out.println("connect");
		IServiceCapableConnection sc = (IServiceCapableConnection)conn;
		sc.invoke("clientMethod", new Object[]{"One",1}, this);
		
		return super.appConnect(conn, params);
	}
	
	
	public void appDisconnect(IConnection conn) {
		log.info("myDemo appDisconnect");
		super.appDisconnect(conn);
	}

	public void resultReceived(IPendingServiceCall arg0) {
		System.out.println("来自客户端的返回值 :" + arg0.getResult());
	}
	
	
}

 

要接收客户端的返回值 需实现 IPendingServiceCallback 接口 重写 resultReceived 方法

 

sc.invoke("clientMethod", new Object[]{"One",1}, this);

 

此处调用了客户端的 clientMethod 方法  参数第一个为字符串 第二个为 数值型  其中 this 为实现了 IPendingServiceCallback 接口的实例对象 此处实现该接口的为本类 所以为 this

 

 

接下来是客户端代码

 

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="Red5 Connect Test" creationComplete="appinit()">
<mx:Script>
  <![CDATA[
  	import mx.controls.Alert;
  import flash.display.Sprite;
   import flash.events.NetStatusEvent;
   import flash.events.SecurityErrorEvent;
   import flash.media.Video;
   import flash.net.NetConnection;
   import flash.net.NetStream;
   import flash.events.Event;


  var command:String="rtmp://localhost/red5Demo2";
  var mync:NetConnection=new NetConnection();

  var client1:Object = new Object();

  function netStatusHandler(sevt:NetStatusEvent):void{
      
  }


  function appinit(){
   mync.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);

  }

  function serverConnect():void{
   out_txt.text="Connectting ... ...";
   client1.clientMethod = this.clientMethod;
   mync.client = this.client1;
   mync.connect(command);
   trace("serverConnect:"+command);
  }

  public function clientMethod(str:String, num:Number):String{
  	Alert.show("str: "+ str +" num:" + num);
  	return "客户端方法被调用....";
  }

  ]]>
</mx:Script>
<mx:Style>
  WindowedApplication
  {
   background-color:"0xffffff";
   background-alpha:"0.5";
  }
</mx:Style>
<mx:Label y="100" text="连接状态:未连接" horizontalCenter="0" fontSize="12" fontWeight="normal" id="out_txt" condenseWhite="true" height="150"/>
<mx:Button y="254" label="点击我连接Red5" fontSize="11" fontWeight="normal" horizontalCenter="0" click="serverConnect()"/>
</mx:WindowedApplication>

 

 

当连接服务端后 服务端会调用客户端的clientMethod 方法 弹出对话框

 

到此服务端调用客户端方法的例子结束

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