在as3套接字中具有多线程或后台进程的as3套接字

时间:2011-11-17 12:45:44

标签: multithreading actionscript-3 sockets flex-mobile

我的项目使用flex mobile 4.5使用套接字..接收庞大的数据流.. 并且ui应用程序就像挂起...在所有过程数据完成之前的一分钟 如何在as3中使用socket中的多线程?任何样本? 任何诡计?制作后台流程

吼叫我的代码


                   

            private var workQueue:Array = new Array();

            /**************** VARIABLES ************************/
            [Bindable]public var defauldSocketIP:String ="119.99.67.168";
            [Bindable]public var defauldPort:Number = 9090;
            [Bindable]public var heartbeatTime:Number = 1000 * 60;
            [Bindable]public var limitLivetrade:Number = 30;
            [Bindable]public var sessionid:String;
            [Bindable]public var userid:String;
            [Bindable]public var password:String;
            [Bindable]public var msgdata:String;
            [Bindable]public var busyIndicator:Boolean = false;
            [Bindable]public var livetradeData:ArrayCollection = new ArrayCollection();
            [Bindable]public var IndicesSectoralData:ArrayCollection = new ArrayCollection();
            [Bindable]public var topGainerBrokerData:ArrayCollection = new ArrayCollection();
            [Bindable]public var brokerData:ArrayCollection = new ArrayCollection();
            [Bindable]public var stockData:ArrayCollection = new ArrayCollection(); 
            [Bindable]public var marketChartData:ArrayCollection = new ArrayCollection(); 
            /* best Quote */
            [Bindable]public var bestQuoteDataA:ArrayCollection = new ArrayCollection();
            [Bindable]public var bestQuoteDataB:ArrayCollection = new ArrayCollection();
            [Bindable]public var bestQuoteBID:ArrayCollection = new ArrayCollection();
            [Bindable]public var bestQuoteOFFER:ArrayCollection = new ArrayCollection();
            [Bindable]public var bestQuoteTFREQ:ArrayCollection = new ArrayCollection();
            [Bindable]public var stockSummarayData:ArrayCollection = new ArrayCollection();
            /* end of best Quote */
            [Bindable]public var tempDataSocket:ByteArray = new ByteArray();
            [Bindable]public var sTemp:int = new int();
            [Bindable]public var db:ArrayCollection = new ArrayCollection();
            [Bindable]public var curentCode:String;//current code for unscribe
            /**************** Socket        ********************/
            private var buffer:ByteArray = new ByteArray();
            public var _data:ByteArray;

            [Bindable]public var websocketData:String;
            [Bindable]public var isLogin:Boolean = false;
            public var flipTrans:FlipViewTransition = new FlipViewTransition();

    \
                   /* socket connection */
            public var socket : Socket;

            public function init() : void {
                //navigator.defaultPushTransition = new FlipViewTransition();
                socket = new Socket();
                socket.addEventListener(Event.CONNECT, onConnect);
                socket.addEventListener(Event.CLOSE, onSocketClose);
                socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
                socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError ); 
                socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSEC_ERRORError );

                /****************** INTERVAL FOR PING SERVER *************/
                var myInterval:uint = setInterval (heartbeat, heartbeatTime);
                //clearInterval(myInterval);
                if(isLogin){
                    flipTrans.duration = 1000; 
                    flipTrans.mode = FlipViewTransitionMode.CARD;
                    flipTrans.direction =  ViewTransitionDirection.RIGHT;
                    mainview.pushView(MainView,null,null,flipTrans);
                }
                /****************** Database setup           *************/
                initDB();
                if(db.length == 0){ createDefauldConfig(); } 

            }
            /**************** menus        ********************/

            /**************** alert information box *********************/
            protected function alertSoketError():void
            {
                Alert.show(this, Alert.IC_INFO, "Information", "Sorry cannot connect to server please contact administrator.");
            }
            /**************** inquotes database      ********************/
            public var sqlConnection:SQLConnection;



                public function onSEC_ERRORError( errorEvent:SecurityErrorEvent ):void 
                { 
                busyIndicator = false;
                isLogin = false;
                mainview.pushView(login);
                trace( "onSEC_ERRORError: " + errorEvent ); 
                alertSoketError();
                }
                public function onSocketClose(e:Event,reconect:Boolean = true):void{                             resetData();
                    socket.close();
                    mainview.pushView(login);
                trace("socket Closed");
                if(reconect){
                reconnect();
                }

                }
                public function onIOError( errorEvent:IOErrorEvent ):void 
                { 
                busyIndicator = false;
                isLogin = false;
                mainview.pushView(login);
                trace( "IOError: " + errorEvent.text ); 
                alertSoketError();
                }
                public function onConnect(event : Event) : void {
                trace("login|"+userid+"|"+(MD5.hash(password)));
                sendData("login|"+userid+"|"+(MD5.hash(password)));
                }
                public function getFormattedTime(t:int):String
                {   
                var s:String = new String();
                var hour:String = t.toString().substr(0,2);
                var minute:String = t.toString().substr(2,2);
                var scn:String = t.toString().substr(4,5);
                return s  =hour+":"+minute+":"+scn;
                }

                public function checkTipe(data:String):void{
                var split:Array = spliter.doSplit(data,"|");
                    /*Login */

                if(split[0]=="login" && split[1]=="ok" && isLogin == false){
                    trace("split[0]"+split[0]+"-split[1]"+split[1]+"isLogin =="+isLogin);

                sessionid = new String();
                sessionid = split[2];
                isLogin = true;
                flipTrans.duration = 1000; 
                flipTrans.mode = FlipViewTransitionMode.CARD;
                flipTrans.direction =  ViewTransitionDirection.RIGHT;
                mainview.pushView(MainView,null,null,flipTrans);
                sendData("subscribe|"+sessionid+"|4|0");
                sendData("subscribe|"+sessionid+"|3|0");
                sendData("subscribe|"+sessionid+"|5|0");
                }
                /*Login fail*/
                if(split[0]=="login" && split[1]!="ok"){

                sessionid = new String();
                Alert.show(this, Alert.IC_INFO, "Login", "Sorry Fail.. check your user name or password");

                    }               
                    /*live trade */
                    if(split[1]=="2"){
                    curentCode = "2";
                        var change:Number = split[17] == 0 ? 0 : split[6] - split[17];
                    var obj:Object      = new Object();
                    obj.header         =  split[0];
                    obj.type           =  split[1];
                    obj.seqno          =  split[2];
                    obj.time           =  FormatTime.getFormattedTime(split[3]);
                    obj.stock          =  split[4];
                    obj.board          =  split[5];
                    obj.last           =  split[6];
                    obj.lot            =  split[7];
                    obj.buyer          =  split[8];
                    obj.buy            =  split[9];
                    obj.seller         =  split[10];
                    obj.sell           =  split[11];
                    obj.bestbid        =  split[12];
                    obj.bestbidlot     =  split[13];
                    obj.bestoffer      =  split[14];
                    obj.bestofferlot   =  split[15];
                    obj.tradeno        =  split[16];
                    obj.previous       =  split[17];
                    obj.change         =  change;
                    obj.percent        =   Number((change * 100 ) / split[17]).toFixed(1);

                    if(livetradeData.length < limitLivetrade){
                    livetradeData.addItem(obj);
                    }else{
                    livetradeData.removeItemAt(livetradeData.length-1);
                    livetradeData.addItemAt(obj,0);
                    }
                    livetradeData.refresh();

                    }
                    if(split[1]=="6"){
                    curentCode = "6" ;
                IndicesSectoralData = IndicesSectoral.setIndicesSectoral(split,IndicesSectoralData);
                IndicesSectoralData.refresh();

                }
                if(split[1]=="TSBB"){
                curentCode = "TSBB";

                var obj:Object      = new Object();
                obj.header         =  split[0];
                obj.type           =  split[1];
                obj.seqno          =  split[2];
                obj.min1           =  split[3];
                obj.min2           =  split[4];
                obj.BrokerID       =  split[5];
                obj.min3           =  split[6];
                obj.BuyAvg         =  split[7];
                obj.SellAvg        =  split[8];
                obj.BuyVol         =  split[9];
                obj.BuyVal         =  formater.formatMoneyUnit(Number(split[10]));
                obj.BuyFreq        =  split[11];
                obj.SellVol        =  split[12];
                obj.SellVal        =  formater.formatMoneyUnit(split[13]);
                obj.SellFreq       =  formater.formatMoneyUnit(split[14]);
                obj.ValNet         =  formater.formatMoneyUnit((Number(split[10]) - 

                Number(split[13])));
                obj.TotalVal       =  formater.formatMoneyUnit((Number(split[10]) + 

                Number(split[13])));
                obj.TotalVol       =  formater.formatMoneyUnit((Number(split[9])  + 

                Number(split[12])));
                obj.FreqTotal      =  formater.formatMoneyUnit((Number(split[11]) + 

                Number(split[14])));

                /*header|type|seqno|-|-|BrokerID|-|BuyAvg|SellAvg|BuyVol|BuyVal|BuyFreq|SellVol|SellVal|SellFreq
                ValNet = BuyVal - SellVal
                TotalVal = BuyVal + SellVal
                TotalVol = BuyVol + SellVol
                FreqTotal = BuyFreq + SellFreq
                Diurut Berdasarkan TotalVal*/
                var brokerIndex:int = find.findIndex(brokerData,"BrokerID", split[5]);
                if(brokerIndex != -1){
                    obj.BrokerName       =  brokerData.getItemAt(brokerIndex).BrokerName;
                }               
                var idx:int = find.findIndex(topGainerBrokerData,"BrokerID", split[5]);
                //trace("BrokerID:"+idx+"arr[5]"+arr[5]);
                if(idx == -1){
                    topGainerBrokerData.addItem(obj);
                }else{
                    topGainerBrokerData.removeItemAt(idx);
                    topGainerBrokerData.addItemAt(obj,idx);
                }


                ArrayCollection. */
                //topGainerBrokerData.sort = numericDataSort;
                topGainerBrokerData.refresh();


                }
                /*broker */
                if(split[1]=="4"){
                curentCode = "4";
                brokerData = Broker.setBroker(split,brokerData);
                brokerData.refresh();
                }
                /*Stock */
                if(split[1]=="3"){

                curentCode = "3";
                var objStock:Object             = new Object();
                objStock.header                 =  split[0];
                objStock.type                   =  split[1];
                objStock.seqno                  =  split[2];
                objStock.stockcode              =  split[3];
                objStock.stockname              =  split[4];
                objStock.status                 =  split[5];
                objStock.stocktype              =  split[6];
                objStock.sector                 =  split[7];
                objStock.ipoprice               =  split[8];
                objStock.baseprice              =  split[9];
                objStock.listedshares           =  split[10];
                objStock.tradelistedshares      =  split[11];
                objStock.shareperlot            =  split[12];

                var idStock:int = find.findIndex(stockData,"stockcode", split[3]);
                //trace("BrokerID:"+idx+"arr[5]"+arr[5]);
                if(idStock == -1){
                    stockData.addItem(objStock);
                }else{
                    stockData.removeItemAt(idStock);
                    stockData.addItemAt(objStock,idStock);
                }

                }

                if(split[1]=="1"){
                curentCode = "1" ;
                trace("best Quote A:"+data+"\n");
                /* best quote bid */
                var XB:Array = spliter.doSplit(data,"<");
                    var bLotBid:String = XB[1].toString().substring(1,XB[1].toString().indexOf("]")-1);
                trace("bLotBid:"+bLotBid);
                if(bLotBid != ""){
                        bLotBid = bLotBid.substring(1,bLotBid.length);
                        var bLotBidArr:Array = spliter.doSplit(bLotBid,">");
                            var bInt:int;
                            for(bInt = 0; bInt < 10;bInt++){
                            if(bLotBidArr[bInt] != undefined){
                            trace("dipotong :"+bLotBidArr[bInt]);
                var bBidDAT:Array = spliter.doSplit(bLotBidArr[bInt],"|");
                    var obLotBid:Object      = new Object();
                    obLotBid.freq        =  formater.formatNumber(bBidDAT[0]);
                    obLotBid.lot         =  formater.formatNumber(bBidDAT[1]);
                    obLotBid.bid         =  formater.formatNumber(bBidDAT[2]);
                    bestQuoteBID.addItem(obLotBid);
                    }

                    }               

                    }

                    /* best quote bid */    
                    /* offer */
                    var bOFF:String = 

                    XB[1].toString().substring(XB[1].toString().indexOf("O[")+2,XB[1].toString().indexOf("]L"));
                trace("bOFF:"+bOFF);

                var boffArr:Array = spliter.doSplit(bOFF,">");
                    var bIof:int;
                    for(bIof = 0; bIof < 10;bIof++){
                    if(boffArr[bIof] != undefined && boffArr[bIof] != ""){
                        trace("dipotong offer :"+boffArr[bIof]);
                var boofDAT:Array = spliter.doSplit(boffArr[bIof],"|");
                    var obLotBidOFF:Object    = new Object();
                    obLotBidOFF.offer         =  formater.formatNumber(boofDAT[0]);
                    obLotBidOFF.lot           =  formater.formatNumber(boofDAT[1]);
                    obLotBidOFF.o             =  formater.formatNumber(boofDAT[2]);
                    bestQuoteOFFER.addItem(obLotBidOFF);
                    }
                    }


                    }                   
                    // best Quote B //

                    if(split[1]=="TP"){
                curentCode = "TP";
                trace("best Quote TP:"+data+"\n");

                //header|type|seqno|stock|board|price|T.Lot|T.Freq
                var objTp:Object             = new Object();
                objTp.header                 =  split[0];
                objTp.type                   =  split[1];
                objTp.seqno                  =  split[2];
                objTp.stock                  =  split[3];
                objTp.board                  =  split[4];
                objTp.price                  =  formater.formatNumber(split[5]);
                objTp.Tlot                   =  formater.formatNumber(split[6]);
                objTp.Tfreq                  =  formater.formatNumber(split[7]);
                bestQuoteTFREQ.addItem(objTp);
                }
                /* stock summary */
                if(split[1]=="5"){
                //curentCode = "5" ;
                var objStcSum:Object                = new Object();
                objStcSum.header                    =  split[0];
                objStcSum.type                      =  split[1];
                objStcSum.seqno                     =  split[2];
                objStcSum.code                      =  split[3];
                objStcSum.board                     =  split[4];
                objStcSum.remarks                   =  split[5];
                objStcSum.previous                  =  split[6];
                objStcSum.highest                   =  split[7];
                objStcSum.lowest                    =  split[8];
                objStcSum.closing                   =  split[9];
                objStcSum.change                    =  split[10];
                objStcSum.tradedvolume              =  split[11];
                objStcSum.tradedvalue               =  split[12];
                objStcSum.tradedfrequency           =  split[13];
                objStcSum.individualindices         =  split[14];
                objStcSum.foreigner                 =  split[15];
                objStcSum.opening                   =  split[16];
                objStcSum.bestbid                   =  split[17];
                objStcSum.bestbidvolume             =  split[18];
                objStcSum.bestoffer                 =  split[19];
                objStcSum.bestoffervolume           =  split[20];
                stockSummarayData.addItem(objStcSum);




                var idStockSum:int = find.findIndex(stockSummarayData,"code", split[3]);

                if(idStockSum == -1){
                stockSummarayData.addItem(objStcSum);
                }else{
                stockSummarayData.removeItemAt(idStockSum);
                stockSummarayData.addItemAt(objStcSum,idStock);
                }
                }
                /* market chart */
                //trace("COMPOSITE-->"+split[1]);
                if(split[0]=="CHI" && split[1]=="COMPOSITE"){
                curentCode = "COMPOSITE";


                //trace("split[1]:"+split[1]+"split[2]:"+split[2]+"split[3]:"+split[3]+"split[4]:"+split[4]);
                var mrc:Array = spliter.doSplit(split.toString(),"\n");

                    if(split[2]!= null){
                    var o:int;
                    for(o = 0;o < (mrc.length -1);o++){

                    var dtmrc:Array = spliter.doSplit(mrc[o],",");

                var objMcht:Object                = new Object();
                if(o == 0){
                objMcht.time                      =  FormatTime.getFormattedTime(dtmrc[2]);
                objMcht.timeUnformat              =  dtmrc[2];
                objMcht.type                      =  dtmrc[3];
                objMcht.value                     =  dtmrc[4];  
                trace("mrc[o]"+mrc[o]+"== dtmrc:"+dtmrc[0]);
                trace("dtmrc[2]"+dtmrc[2]);
                trace("MARKET CHART time 0-->"+FormatTime.getFormattedTime(dtmrc[2]));
                }else{
                objMcht.time                      =  FormatTime.getFormattedTime(dtmrc[0]);
                objMcht.timeUnformat              =  dtmrc[0];
                objMcht.type                      =  dtmrc[1];
                objMcht.value                     =  dtmrc[2];
                //trace("dtmrc[0]"+dtmrc[0]);
                //trace("MARKET CHART time1-->"+FormatTime.getFormattedTime(dtmrc[0]))+"-->value-->"+dtmrc[2];
                }

                marketChartData.addItem(objMcht);


                }
                //time|type|value
                }   
                marketChartData.refresh();

                }

                }

                public function onSocketData(event : ProgressEvent) : void {
                busyIndicator = true;
                var msgdataTemp:String = new String();
                var n:int = socket.bytesAvailable;
                var bArray:ByteArray = new ByteArray();
                try{
                        //trace("n "+n.toString());
                        while(n > 0){
                        var p:int = socket.bytesAvailable;
                        var s:int = new int();
                        if(sTemp == 0){
                        if(p > 0)
                            p = socket.bytesAvailable;
                            try{
                            s = socket.readInt();//total
                            }catch(e:EOFError){
                            trace("err :"+e);
                            break;
                            }
                            p = socket.bytesAvailable;

                            //trace("bArray"+bArray.toString()+" s:"+s.toString()+" "+p);
                            if(s > p){
                                var bytex:ByteArray = new ByteArray();
                                socket.readBytes(bytex,0,p);
                                tempDataSocket.writeBytes(bytex,0,bytex.length);
                                sTemp = s;
                                break;
                            }
                        socket.readBytes(bArray,0,s);
                        bArray.uncompress(CompressionAlgorithm.ZLIB);
                        checkTipe(bArray.toString());
                        n = n-( 4+ s);
                        }else{

                        if(sTemp > p){
                        //bArray.writeBytes(tempDataSocket,0,bArray.length);
                        socket.readBytes(bArray,0,p);
                        tempDataSocket.writeBytes(bArray,0,bArray.length);
                        // trace("sTemp > p  sTemp :"+sTemp+" S :"+s+" p:"+p+" n:"+n+" "+tempDataSocket.length);
                                        sTemp = sTemp +s;
                                        if(sTemp == tempDataSocket.length){
                                            tempDataSocket.uncompress(CompressionAlgorithm.ZLIB);
                                            checkTipe(tempDataSocket.toString());
                                        }

                                        break;
                                    }

                                    //trace("sTemp :"+sTemp+" S :"+s+" p:"+p+" n:"+n+" tempDataSocket :"+tempDataSocket.length+" bArray.length:"+bArray.length);
                        var bnew:ByteArray = new ByteArray();
                        if(socket.bytesAvailable > 0){
                        socket.readBytes(bArray,0,(sTemp - tempDataSocket.length));
                        }

                        //tempDataSocket.writeBytes(bArray,tempDataSocket.length,bArray.length);
                        //bnew.writeBytes(bArray,sTemp
                        bnew.writeBytes(tempDataSocket,0,tempDataSocket.length);
                        bnew.writeBytes(bArray,0,bArray.length);

                        bnew.uncompress(CompressionAlgorithm.ZLIB);
                        //trace("bnew:"+bnew.toString());
                        checkTipe(bnew.toString());
                        tempDataSocket = new ByteArray();
                        n = socket.bytesAvailable;
                        sTemp = 0;

                        }

                        p = socket.bytesAvailable;
                        if(p>0){

                        }


                        }


                }catch(ev:Error){
                trace("socket_error:"+ev.getStackTrace());          

                }finally{

                }
                }
                public function resetData():void{
                    sessionid = "";
                    msgdata = "";
                    busyIndicator = false;
                    livetradeData = new ArrayCollection();
                    IndicesSectoralData = new ArrayCollection();
                    topGainerBrokerData = new ArrayCollection();
                    brokerData = new ArrayCollection();
                    stockData = new ArrayCollection(); 
                    marketChartData = new ArrayCollection(); 

                    bestQuoteDataA = new ArrayCollection();
                    bestQuoteDataB = new ArrayCollection();
                    bestQuoteBID = new ArrayCollection();
                    bestQuoteOFFER = new ArrayCollection();
                    bestQuoteTFREQ = new ArrayCollection();
                    stockSummarayData = new ArrayCollection();

                    tempDataSocket = new ByteArray();
                    sTemp = new int();

                    curentCode = "";//current code for unscribe
                    buffer = new ByteArray();
                    _data = new ByteArray();

                    websocketData  = "";
                    isLogin = false;
                }

                public function trim( s:String ):String
                {
                return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
                }
                private function removeBufferBefore(pos:int):void {
                if (pos == 0) return;
                var nextBuffer:ByteArray = new ByteArray();
                buffer.position = pos;
                buffer.readBytes(nextBuffer);
                buffer = nextBuffer;
                }

                public function unSubscribe():void{
                if(curentCode != null && sessionid  != null && curentCode != ""){
                    try{
                    socket.writeMultiByte("unsubscribe|"+sessionid+"|"+curentCode,"iso-8859-1");
                socket.writeByte(0xff);     
                socket.flush();
                }catch(e:flash.events.ErrorEvent){
                Alert.show(this, Alert.IC_INFO, "Sorry..", e.text);
                    }
                    }
                    }           
                    public function sendData(data:String):void{
                    busyIndicator = true;
                    unSubscribe();
                    socket.writeMultiByte(data,"iso-8859-1");
                    socket.writeByte(0xff);     
                    socket.flush();
                    trace("sendData:"+data);

                }
                public function connect(useridx:String,passwordx:String) : void {
                busyIndicator = true;
                userid   = useridx;
                password = passwordx;
                trace("userid:"+userid);
                trace("password:"+password);
                socket.connect(db.getItemAt(0).defauldIpAdress, db.getItemAt(0).defauldPort);
                //threadedSocket();
            }


        ]]>
    </fx:Script>

    <s:ViewNavigator label="Main" backgroundColor="0x000"  id="mainview" width="100%" height="100%" 

                     firstView="views.login" icon="@Embed('assets/home.png')"/>
    <s:ViewNavigator label="Menu" backgroundColor="0x000"  width="100%"  height="100%" firstView="views.MenuView" 

                     icon="@Embed('assets/menu.png')"/>
    <s:ViewNavigator label="about" backgroundColor="0x000" width="100%" height="100%" firstView="views.aboutView" 

                     icon="@Embed('assets/info.png')"/>

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <!--<mx:NumberFormatter
        decimalSeparatorFrom="."
        decimalSeparatorTo="."
        precision="-1"
        rounding="none|up|down|nearest"
        thousandsSeparatorFrom=","
        thousandsSeparatorTo=","
        useNegativeSign="true|false"
        useThousandsSeparator="true|false"/>-->

        <mx:NumberFormatter id="fortmar1" precision="6"
                            rounding="none"
                            decimalSeparatorFrom="."
                            decimalSeparatorTo="."
                            thousandsSeparatorTo=","
                            thousandsSeparatorFrom=","
                            useThousandsSeparator="true"
                            useNegativeSign="true"
                            />

    </fx:Declarations>

</s:TabbedViewNavigatorApplication>

2 个答案:

答案 0 :(得分:1)

如果您遇到旧版本的Flash播放器,则需要在AS3中搜索谷歌&#34;绿色线程&#34;。

如果您使用的是较新版本的播放器,或Adobe AIR用于桌面和设备,则可以使用多线程,通过&#34; actionscript worker&#34 ;.

答案 1 :(得分:-2)

Flash和Flex引擎只能在一个线程中运行 - 无法进行多线程或并行编程。最接近的是Flash的Event系统;您可以异步加载资产。我无法确定你是否这样做,因为你的代码有点难以理解。

我的建议 - 将你的装载包装在另一个班级。这应该有助于将其与用户界面分开,这可能会改善您的情况和代码质量。