隨筆-4  評論-7  文章-0  trackbacks-0
            2012年2月13日
          Array排序
          protected function applicationCompleteHandler(event:FlexEvent):void
          {
              var array:Array = [];
              array.push(new Vga("a",10));
              array.push(new Vga("c",2));
              array.push(new Vga("f",1.3));
              array.push(new Vga("d",1.1));
              array.push(new Vga("e",16));
              array.push(new Vga("b",0));
              trace(array.toString());
              //output:   [a,10],[c,2],[f,1.3],[d,1.1],[e,16],[b,0]
              var defaultSort:Array = array.sort();//默認排序
              trace(defaultSort.toString());
              //output:   [a,10],[b,0],[c,2],[d,1.1],[e,16],[f,1.3]
              var sortFunArray:Array = array.sort(sortFun);//使用自定義方法排序
              trace(sortFunArray.toString());
              //output:   [b,0],[d,1.1],[f,1.3],[c,2],[a,10],[e,16]
          }

          /**自定義排序方法*/            
          public function sortFun(a:Vga,b:Vga):int{
              if(a.price < b.price){
              return -1; //a在前,b在后
              }else if(a.price == b.price){
              return 0; //ab位置不變
              }else{
              return 1; //b在前,a在后
              }
          }



          /**排序VO對象*/
          public class Vga
          {
              public var name:String;
              public var price:Number;
              
              public function Vga(name:String,price:Number)
              {
              this.name = name;
              this.price = price;
              }
                  
              public function toString():String{
              return "["+this.name+","+this.price+"]";
              }
          }


          ArrayCollection排序
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Script>
                  <![CDATA[
                      import mx.collections.SortField;
                      import mx.collections.Sort;
                      import mx.collections.ArrayCollection;
                      private var acSort:ArrayCollection=
                      new ArrayCollection([{id:0,userName:"zhangSan",age:21},
                                          {id:2,userName:"liSi",age:24},
                                          {id:1,userName:"wangWu",age:31}]);
                      
                      
                      private function sortAc():ArrayCollection{
                          var sort:Sort=new Sort();
                          //按照ID升序排序
                          sort.fields=[new SortField("id")];
                          
                          //按照userName降序排序
                          sort.fields=[new SortField("userName",true,true)];
                          
                          //先按ID升序,再按userName降序
                          sort.fields[new SortField("id"),new SortField("userName",true,true)];
                          acSort.sort=sort;
                          acSort.refresh();//更新
                          return acSort;
                      }
                      
                      /*
                      其實看看API就一目了然
                      SortField () 構造函數 

                      public function SortField(name:String = null, 
                                  caseInsensitive:Boolean = false, 
                                  descending:Boolean = false, 
                                  numeric:Object = null)
                                              
                      參數  
                      name:String (default = null) — 此字段用來進行比較的屬性的名稱。如果該對象為簡單類型,則傳遞 null。 
                      caseInsensitive:Boolean (default = false) — 在對字符串進行排序時,指示比較運算符是否忽略值的大小寫。
                      descending:Boolean (default = false) — 指示比較運算符是否按降序排列項目。  
                           
                      numeric:Object (default = null) — 指示比較運算符是否按編號而不按字母順序比較排序項目。 
                      
          */            
                  ]]>
              </mx:Script>
          </mx:Application>
          posted @ 2013-03-17 12:19 wkkyo 閱讀(4430) | 評論 (0)編輯 收藏

          項目中需要用到openfire的文件傳輸,但是客戶端使用flex,官方提供的xiff包中并沒有封裝文件傳輸的功能,沒辦法,研究了幾天,在google和官方smock源碼的幫助下終于實現了xiff下的文件傳輸,在這里做個總結。

          openfire服務器是基于xmpp協議的,XMPP支持兩種文件流傳輸協議,SOCKS5 Bytestreams和 In-Band Bytestreams,SOCKS5是直接發送二進制流,而IBB是將文件轉成base64碼進行然后用message的形式進行傳輸,我這里僅實現了SOCKS5的文件代理傳輸。
          SOCKS5文件傳輸需要用到兩個協議,XEP-0065和XEP-0096
          XEP-0096定義文件傳輸協議,提供了一個模塊化框架使能交換被傳輸文件的信息以及參數的協商,也就是在傳輸文件之前協商將要傳輸的文件信息。
          XEP-0065定義SOCKS5流傳輸標準協議,提供用于在任意兩個XMPP用戶之間建立字節流并進行文件傳輸。
          根據我的理解,文件傳輸的過程分為協商,建立socks5連接,二進制傳輸這三個階段
          協商的過程最復雜,然后是建立連接,傳輸就比較簡單,下面一個一個來講
          協商包括初始方、目標方、代理方,初始方就是發送文件方,目標方即文件接收方,代理方是socks5代理服務器,

          協商過程就是三方互相發送xml來交換信息的過程,通俗點就是三個人溝通一下傳什么文件和怎么傳文件。
          首先遵循XMP-0096協議,初始方給目標方發送包含文件信息的xml

          <iq to="android@192.168.1.113/Spark 2.6.3" type="set" id="iq_13" from="iphone@192.168.1.113/xiff">
              
          <si profile="http://jabber.org/protocol/si/profile/file-transfer" mime-type="text/plain" id="82B0C697-C1DE-93F9-103E-481C8E7A3BD8" xmlns="http://jabber.org/protocol/si">
                  
          <feature xmlns="http://jabber.org/protocol/feature-neg">
                      
          <xmlns="jabber:x:data" type="form">
                          
          <field var="stream-method" type="list-single">
                              
          <option><value>http://jabber.org/protocol/bytestreams</value></option>
                              
          <option><value>http://jabber.org/protocol/ibb</value></option>
                          
          </field>
                      
          </x>
                  
          </feature>
                  
          <file xmlns="http://jabber.org/protocol/si/profile/file-transfer" name="img0545.png" size="152443"><desc>send</desc></file>
              
          </si>
          </iq>

           
          目標方接收到信息后發送回執,表示同意接收文件

           

          <iq id="iq_13" to="iphone@192.168.1.113/xiff" from="android@192.168.1.113/Spark 2.6.3" type="result">
              
          <si xmlns="http://jabber.org/protocol/si">
                  
          <feature xmlns="http://jabber.org/protocol/feature-neg">
                      
          <xmlns="jabber:x:data" type="submit">
                          
          <field var="stream-method">
                               
          <value>http://jabber.org/protocol/bytestreams</value>
                               
          <value>http://jabber.org/protocol/ibb</value>
                          
          </field>
                       
          </x>
                  
          </feature>
               
          </si>
          </iq>

           

          這時進入XEP-0065協議階段
          初始方給服務器發送信息,請求提供代理服務器

           

          <iq id="iq_15" type="get"><query xmlns="http://jabber.org/protocol/disco#items" /></iq>


          服務器回復信息,告知可用的代理

           

          <iq type="result" id="iq_15" to="iphone@192.168.1.113/xiff">
              
          <query xmlns="http://jabber.org/protocol/disco#items">
                  
          <item jid="proxy.192.168.1.113" name="Socks 5 Bytestreams Proxy"/>
                  
          <item jid="pubsub.192.168.1.113" name="Publish-Subscribe service"/>
                  
          <item jid="conference.192.168.1.113" name="公共房間"/>
                  
          <item jid="search.192.168.1.113" name="User Search"/>
              
          </query>
          </iq>

           
          這里選擇name=“Socks 5 Bytestreams Proxy”的代理,初始方給這個代理發送信息獲取代理連接信息

           

          <iq id="iq_17" to="proxy.192.168.1.113" type="get"><query xmlns="http://jabber.org/protocol/bytestreams" /></iq>

          代理方回復信息,告知初始方代理的jid、IP、端口等信息

           

          <iq type="result" id="iq_17" from="proxy.192.168.1.113" to="iphone@192.168.1.113/xiff">
              
          <query xmlns="http://jabber.org/protocol/bytestreams">
                  
          <streamhost jid="proxy.192.168.1.113" host="192.168.1.113" port="7777"/>
              
          </query>
          </iq>

           
          初始方收到代理信息后將代理的信息發送給目標方

           

          <iq to="android@192.168.1.113/Spark 2.6.3" type="set" id="iq_19" from="iphone@192.168.1.113/xiff">
              
          <query xmlns="http://jabber.org/protocol/bytestreams" mode="tcp" sid="82B0C697-C1DE-93F9-103E-481C8E7A3BD8">
                  
          <streamhost port="7777" host="192.168.1.113" jid="proxy.192.168.1.113" />
              
          </query>
          </iq>

           

          然后就進入連接階段,也就是初始方和目標方分別和代理建立socks5連接的過程。(關于SOCKS5協議連接,我之后會補充)。
          目標方收到代理信息后和代理建立socket連接(使用SOCKS5協議連接),連接成功后通知初始方使用的代理jid

           

          <iq id="iq_19" to="iphone@192.168.1.113/xiff" type="result" from="android@192.168.1.113/Spark 2.6.3">
              
          <query xmlns="http://jabber.org/protocol/bytestreams">
                  
          <streamhost-used jid="proxy.192.168.1.113"/>
              
          </query>
          </iq>

           
          初始方開始與代理建立socket連接(也使用SOCKS5協議),連接成功后給代理發送請求,要求激活文件流

           

          <iq to="proxy.192.168.1.113" type="set" id="iq_21" from="iphone@192.168.1.113/xiff">
          <query xmlns="http://jabber.org/protocol/bytestreams" sid="82B0C697-C1DE-93F9-103E-481C8E7A3BD8">
          <activate>android@192.168.1.113/Spark 2.6.3</activate>
          </query>
          </iq>

           
          代理回復激活成功信息

           

          <iq type="result" id="iq_21" from="proxy.192.168.1.113" to="iphone@192.168.1.113/xiff"/>

          初始方收到回復信息后就進入二進制流傳輸階段,這時就可以開始發送二進制流了

          等初始方將流發送完畢后把socket流關閉傳輸就完成了文件的傳輸。

           

          注意:type為result的回復信息使用的id一定要和請求的信息id一樣。

           

          posted @ 2012-02-13 00:55 wkkyo 閱讀(8581) | 評論 (5)編輯 收藏
          主站蜘蛛池模板: 静安区| 喜德县| 龙州县| 东丰县| 马鞍山市| 教育| 长白| 察隅县| 兰溪市| 紫金县| 昆明市| 共和县| 田东县| 铜梁县| 育儿| 安龙县| 衡山县| 赣榆县| 长白| 茂名市| 商河县| 永寿县| 淄博市| 洛扎县| 宿州市| 甘孜县| 常州市| 丰顺县| 香格里拉县| 重庆市| 和顺县| 如东县| 平远县| 镇沅| 三河市| 凉城县| 宜城市| 津市市| 永春县| 通化县| 晋州市|