在tomcat下开发Red5应用

来源:互联网 发布:openstack源码 编辑:程序博客网 时间:2024/05/18 17:25

 

客户端和服务器端的方法相互调用比较重要,在线列表基本上全是用这种方式实现的,当然也有使用RemoteSharedObject来实现的,但本人不太喜欢用RemoteSharedObject,只是用RemoteSharedObject来进行广播等操作。

1.编辑第二篇(抛弃LCDS和FMS,在tomcat下开发Red5应用(第二篇)-建立新的Red5应用)中的Application.java:

Java代码 复制代码
  1. package red5.example.red5server;   
  2.   
  3. import org.red5.server.adapter.ApplicationAdapter;   
  4. import org.red5.server.api.IConnection;   
  5. import org.red5.server.api.Red5;   
  6. import org.red5.server.api.service.IServiceCapableConnection;   
  7.   
  8. public class Application extends ApplicationAdapter {   
  9.   
  10.     private String userName;   
  11.        
  12.     //客户端调用的方法   
  13.     public String callFromClient(String userName) {   
  14.         this.userName = userName;   
  15.         callClient();   
  16.         return "Hello:"+userName;   
  17.     }   
  18.        
  19.     //服务器端调用客户端的方法   
  20.     public void callClient() {   
  21.         IConnection conn=Red5.getConnectionLocal();   
  22.         if (conn instanceof IServiceCapableConnection) {   
  23.             IServiceCapableConnection sc = (IServiceCapableConnection) conn;   
  24.             sc.invoke("callFromServer"new Object[]{"hi,"+userName+" this message from server"});   
  25.         }   
  26.     }   
  27. }  

PS:记得将编译好的class文件放入webapps/ROOT/WEB-INF/classes。

2.编辑第二篇(抛弃LCDS和FMS,在tomcat下开发Red5应用(第二篇)-建立新的Red5应用)中的red5client001.mxml:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import flash.net.*;  
  6.             import flash.events.*;     
  7.             import flash.utils.*;     
  8.             import mx.controls.*;   
  9.               
  10.             private var nc:NetConnection;  
  11.               
  12.             public function init():void {  
  13.                 nc = new NetConnection();                    
  14.                 nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);     
  15.                 nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, netSecurityError);     
  16.                 nc.connect("rtmp://localhost/red5Server");     
  17.                 nc.client = this;   
  18.             }  
  19.               
  20.             private function netStatus(event:NetStatusEvent):void {     
  21.                 var connStatus:String = event.info.code;  
  22.                 //Alert.show(connStatus);   
  23.                 if(connStatus == "NetConnection.Connect.Success") {    
  24.                     nc.call("callFromClient",new Responder(callServerMethodResult,callServerMethodFault),Math.random().toString());  
  25.                 }  
  26.             }     
  27.              
  28.             private function netSecurityError(event:SecurityErrorEvent):void {     
  29.                 Alert.show("netSecurityError: " + event);     
  30.             }  
  31.               
  32.             public function callServerMethodResult(re:String):void {  
  33.                 Alert.show("客户端调用服务器端方法成功,返回结果:"+re);  
  34.             }  
  35.               
  36.             public function callServerMethodFault(fo:Object):void {  
  37.                 Alert.show("客户端调用服务器端方法失败:"+fo.toString());  
  38.             }  
  39.               
  40.             public function callFromServer(re:Object):void {  
  41.                 Alert.show("服务器端调用客户端方法,传递的参数为:"+re.toString());  
  42.             }  
  43.         ]]>  
  44.     </mx:Script>  
  45. </mx:Application>  

将编译好的swf拷贝到webapps/red5Server目录下。

3.重新启动tomcat 运行浏览器,在地址栏输入http://localhost:8080/red5Server/red5client001.html 看到弹出窗口了吧。

 

客户端和服务器端方法相互调用就这么简单,实际上原有的基于FMS开发的Flex客户端代码修改量是非常小的。在下一篇将讲解客户端和服务器端方法调用中的参数传递。

原创粉丝点击