Flex客户端与Red5通信示例

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

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

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

[java]  view plain copy
  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>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值