---
title: "flex temoting"
author: "bluelzx"
date: "2008-06-10T17:46:52Z"
tags: ["Ajax", "Flex", "WebService", "json", "网络协议"]
slug: "flex-temoting"
draft: false
lastmod: "2008-06-10T17:46:52Z"
id: 201876
url: "https://bluelzx.iteye.com/blog/201876"
---

Flex 的Remoting采用AMF通信协议（Flex3已经升级为了AMF2了），这种协议最大的一个优势就是由于采用了二进制的传输方式，其传输效率非常 高，可以让我们在复杂的网络环境获得良好的性能。根据国外一个很牛的老外写的在线测试工具，AMF协议的效率要远远高于webservice以及采用 JSON格式承载数据的AJAX方式。![](http://www.lightria.cn/wp-content/plugins/fckeditor_for_wordpress/smiles/msn/wink_smile.gif)  Flex的另一种方式通信就是PollingChannel,它的特点就是它可以多批次的去服务端取数据，而不是长时间保持连接直到所有数据取回之后才关闭我们宝贵的HTTP连接。

       首先我们在WEB-INF下面的Class目录中添加一个JAVA类，如下：

     public class HelloWorld {  
         private [String](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3AString%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) userSaid;  
        public [String](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3AString%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) repeat( [String](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3AString%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) said) {  
            this .userSaid = "Reply from server: " + said;  
            return this .userSaid ;  
        }  
        public [String](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3AString%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) sayHello( ) {  
        [Date](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3ADate%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) now = new [Date](http://www.google.com/search?hl=en&amp%3Bq=allinurl%3ADate%20java.sun.com&amp%3BbntI=I%27m%20Feeling%20Lucky) ( ) ;  
        return "Hello World " + now;  
     }  
  }

        然后我们打开Remoting-Config.xml文件，在service中添加一个目的地指向我们服务端的JAVA类（destination节点），具体如下：

        &lt;service id ="remoting-service" class ="flex.messaging.services.RemotingService" &gt;  
        &lt;adapters&gt;  
        &lt;adapter-definition id ="java-object" class ="flex.messaging.services.remoting.adapters.JavaAdapter" default ="true" &gt; &lt;/adapter-definition&gt;  
       &lt;/adapters&gt;  
      &lt;destination id ="Hello" &gt;  
       &lt;properties&gt;  
         &lt;source&gt; HelloWorld&lt;/source&gt;  
       &lt;/properties&gt;  
      &lt;/destination&gt;  
    &lt;default-channels&gt;  
        &lt;channel ref ="my-amf" &gt;  
    &lt;/channel&gt;  
&lt;/default-channels&gt;  
&lt;/service&gt;

       添 加完成之后，我们打开一个flex页面，在上面添加一个RemotObject标签，当然你也可以在可视化状态下拖一个上去，然后添加我们刚才在 HelloWorl类中定义的两个方法，并在AS代码块中定义两个回调方法用来异步接收返回的结果或发生的异常，具体代码如下：

       &lt;mx:application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundcolor ="#FFFFFF" viewsourceurl="srcview/index.html" &gt;  
    &lt;mx:remoteobject id="myservice" fault="faultHandler(event)" showbusycursor="true" destination="Hello" &gt;  
        &lt;mx:method name ="sayHello" result="resultHandler(event)" &gt;  
        &lt;mx:method name ="repeat" result="resultHandler(event)" &gt;  
    &lt;/mx:method&gt;  
   
    &lt;mx:script&gt;  
        &lt;!–[ CDATA[  
            import mx.managers .CursorManager ;  
            import mx.rpc .events .ResultEvent ;  
            import mx.rpc .events .FaultEvent ;  
            private function faultHandler( fault:FaultEvent) :void  
            {  
                CursorManager.removeBusyCursor ( ) ;  
                result\_text.text = "code:n" + fault.fault .faultCode + "nnMessage:n" + fault.fault .faultString + "nnDetail:n" + fault.fault .faultDetail ;  
            }  
   
            private function resultHandler( evt:ResultEvent) :void  
            {  
                result\_text.text = evt.message .body .toString ( ) ; // same as: evt.result.toString();  
            }  
        ] ] –&gt;  
    &lt;/mx:script&gt;  
   
    &lt;mx:button x="250" y="157" label="sayHello" width ="79" click="myservice.getOperation(’sayHello’).send();" &gt;  
    &lt;mx:button x="250" y="187" label="Repeat" click="myservice.getOperation(’repeat’).send(myText.text); " &gt;  
    &lt;mx:textarea x="10" y="36" width ="319" height ="113" id="result\_text" &gt;  
    &lt;mx:label x="10" y="10" text ="Result:" &gt;  
    &lt;mx:textinput x="82" y="187" id="myText" text ="Sent to Server" &gt;  
&lt;/mx:textinput&gt;  
&lt;/mx:label&gt;

&lt;/mx:textarea&gt; &lt;/mx:button&gt;&lt;/mx:button&gt;&lt;/mx:method&gt;&lt;/mx:remoteobject&gt;&lt;/mx:application&gt;

     做好之后，激动人心的时刻来临了，![](http://www.lightria.cn/wp-content/plugins/fckeditor_for_wordpress/smiles/msn/shades_smile.gif)  F11运行，现在你就可以尽情的与服务端发消息了。注意，JAVA类的序列化与反序列化对我们来说都是透明的，这一切都是FLEX与Blazeds为我们做的。这就是它们带给我们的好处，让我们将重点放在业务逻辑与用户更好的操作性与体验上。