·您当前的位置:主页 > 技术教程 > RED5教程 >

[RED5]实现Flex与Red5的通讯源代码示例

时间:2014-07-08 08:48酷播
服务器端Application提供一个供Flex客户端远程调用的方法getMyName,代码如下

服务器端Application提供一个供Flex客户端远程调用的方法getMyName,代码如下:

  1. package org.chy.flex02; 
  2. import org.red5.server.adapter.ApplicationAdapter; 
  3. public class Application extends ApplicationAdapter{ 
  4.          
  5.         public String getMyName(String id){ 
  6.             if("001".equals(id)){ 
  7.                 return "ChenYang"; 
  8.             } 
  9.             return "JAVA&Flex"; 
  10.         } 
  11.  

客户端Flex程序,当用户点击'get name'按钮时,远程调用getMyName方法。响应给客户端

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="355" height="176" applicationComplete="init()"> 
  3.     <mx:Script> 
  4.         <!--[CDATA[ 
  5.              
  6.             import flash.net.NetConnection; 
  7.              
  8.             import flash.net.NetStream; 
  9.              
  10.             private var nc:NetConnection 
  11.              
  12.             private function init():void{ 
  13.                 nc = new NetConnection(); 
  14.                 nc.addEventListener(NetStatusEvent.NET_STATUS,connectHandler); 
  15.                 nc.client = this
  16.                 nc.connect("rtmp://localhost:1935/Flex02"); 
  17.             } 
  18.              
  19.             private function getName():void{ 
  20.                 nc.call("getMyName",new Responder(okHandle,failureHandle),"001"); 
  21.             } 
  22.              
  23.             public function okHandle(result:String):void{ 
  24.                 trace("ok:"+result); 
  25.                 myname.text=result
  26.             } 
  27.              
  28.             public function failureHandle(result:String):void{ 
  29.                 trace("failure:"+result); 
  30.             } 
  31.              
  32.             private function connectHandler(evt:NetStatusEvent):void{ 
  33.                 trace("code:"+evt.info.code); 
  34.                 if(evt.info.code != "NetConnection.Connect.Success"){ 
  35.                     nc.removeEventListener(NetStatusEvent.NET_STATUS,connectHandler); 
  36.                 } 
  37.             } 
  38.              
  39.             public function onBWDone():void{ 
  40.              
  41.             } 
  42.              
  43.         ]]--> 
  44.     </mx:Script> 
  45.      
  46.     <mx:Button x="36" y="72" label="get name" click="getName()"/> 
  47.      
  48.     <mx:TextInput x="124" y="72" width="173" id="myname"/> 
  49.      
  50. </mx:Application> 

.

热门文章推荐

请稍候...