随笔-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 阅读(4406) | 评论 (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 阅读(8542) | 评论 (5)编辑 收藏