Ignore:
Timestamp:
03/19/08 17:15:13 (16 years ago)
Author:
niltonneto
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/instant_messenger/js/connector.js

    r151 r235  
    11function Connector() 
    22{ 
     3        this.__HTTP__ = []; 
     4        this.__THREADS__ = []; 
    35} 
    46 
     
    79        { 
    810                // connection 
    9                 var __HTTP__ = null; 
    10                 var __THREADS__ = []; 
     11                //var __HTTP__ = null; 
     12                //var __THREADS__ = []; 
    1113                var index = 0; 
    1214                var rtlen = 0; 
    1315                var ie = false; // ie don't has stream 
    14                 function makeConnection() 
    15                 { 
    16                         if ( __HTTP__ == null ) 
    17                                 __HTTP__ = xhr(); 
     16                var __this__ = this; 
     17                function makeConnection(pThread) 
     18                { 
     19                        //if ( __this__.__HTTP__ === false ) 
     20                                __this__.__HTTP__[pThread] = xhr(); 
    1821                } 
    1922                function setPost(pThread) 
    2023                { 
    21                         __THREADS__[pThread].__METHOD__ = 'POST'; 
     24                        __this__.__THREADS__[pThread].__METHOD__ = 'POST'; 
    2225                        return true; 
    2326                } 
    24                 // this set __THREADS__[?].__TYPE__ to 4 for request or 3 for stream 
     27                // this set __this__.__THREADS__[?].__TYPE__ to 4 for request or 3 for stream 
    2528                function setStream(pThread) 
    2629                { 
    27                         __THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4; 
     30                        __this__.__THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4; 
    2831                        return true; 
    2932                } 
     
    8184                } 
    8285 
    83                 var __HANDLER__ = function() 
     86                function __HANDLER__() 
    8487                { 
    8588                        try 
    8689                        { 
    87                                 if ( ie && __HTTP__.readyState != 4 ) 
     90                                if ( ie && __this__.__HTTP__[arguments[0]].readyState != 4 ) 
    8891                                        return false; 
    8992 
    90                                 var threadId = __HTTP__.getResponseHeader('ThreadId'); 
     93                                //var threadId = __this__.__HTTP__[arguments[0]].getResponseHeader('ThreadId'); 
     94                                var threadId = arguments[0]; 
     95 
    9196                                if ( !threadId ) 
    9297                                        return false; 
    9398 
    94                                 var type = __THREADS__[threadId].__TYPE__; 
    95                                 if ( __HTTP__.readyState == 3 || __HTTP__.readyState == 4 ) 
    96                                 { 
    97                                         if ( !ie && __HTTP__.readyState == 3 && __THREADS__[threadId].__HANDLER__.stream ) 
    98                                         { 
    99                                                 var data = trim(__HTTP__.responseText.substr(index)); 
    100                                                 rtlen = __HTTP__.responseText.length; 
     99                                if ( __this__.__HTTP__[threadId].readyState == 3 || __this__.__HTTP__[threadId].readyState == 4 ) 
     100                                { 
     101                                        if ( !ie && __this__.__HTTP__[threadId].readyState == 3 && __this__.__THREADS__[threadId].__HANDLER__.stream ) 
     102                                        { 
     103                                                var data = trim(__this__.__HTTP__[threadId].responseText.substr(index)); 
     104                                                rtlen = __this__.__HTTP__[threadId].responseText.length; 
    101105                                                if ( index < rtlen && data.length ) 
    102106                                                { 
    103                                                         __THREADS__[threadId].__HANDLER__.stream(data); 
     107                                                        __this__.__THREADS__[threadId].__HANDLER__.stream(data); 
    104108                                                        index = rtlen; 
    105109                                                } 
    106110                                        } 
    107                                         if ( __THREADS__[threadId].__HANDLER__.request && __HTTP__.readyState == 4 ) 
    108                                         { 
    109                                                 switch ( __HTTP__.status ) 
     111                                        if ( __this__.__THREADS__[threadId].__HANDLER__.request && __this__.__HTTP__[threadId].readyState == 4 ) 
     112                                        { 
     113                                                switch ( __this__.__HTTP__[threadId].status ) 
    110114                                                { 
    111115                                                        case 200: 
    112                                                                 var data = ( __THREADS__[threadId].__MODE__ == 'XML' ) ? 
    113                                                                         __HTTP__.responseXML : 
    114                                                                         __HTTP__.responseText; 
    115  
    116                                                                 if ( ie && __THREADS__[threadId].__HANDLER__.stream ) 
    117                                                                         __THREADS__[threadId].__HANDLER__.stream(data); 
    118  
    119                                                                 if ( __THREADS__[threadId].__HANDLER__.request ) 
    120                                                                         __THREADS__[threadId].__HANDLER__.request(data); 
     116                                                                var stream = false; 
     117                                                                var request = false; 
     118                                                                var data = ( __this__.__THREADS__[threadId].__MODE__ == 'XML' ) ? 
     119                                                                        __this__.__HTTP__[threadId].responseXML : 
     120                                                                        __this__.__HTTP__[threadId].responseText; 
     121 
     122                                                                if ( ie && __this__.__THREADS__[threadId].__HANDLER__.stream ) 
     123                                                                        stream = __this__.__THREADS__[threadId].__HANDLER__.stream; 
     124 
     125                                                                if ( __this__.__THREADS__[threadId].__HANDLER__.request ) 
     126                                                                        request = __this__.__THREADS__[threadId].__HANDLER__.request; 
     127 
     128                                                                delete __this__.__THREADS__[threadId]; 
     129 
     130                                                                if ( stream ) 
     131                                                                        stream(data); 
     132 
     133                                                                if ( request ) 
     134                                                                        request(data); 
    121135 
    122136                                                                break; // [case : status 200] 
     
    128142                                        } 
    129143                                } 
    130                                 if ( __HTTP__.readyState == 4 ) 
    131                                 { 
    132                                         delete __THREADS__[threadId]; 
    133                                 } 
     144                                //if ( __this__.__HTTP__.readyState == 4 ) 
     145                                //{ 
     146                                //      delete __this__.__THREADS__[threadId]; 
     147                                //} 
    134148                        } 
    135149                        catch(e) 
    136150                        { 
    137151                                //alert("__HANDLER__\n" + threadId + "\n " + e + "\n ErroR: " + e.description); 
    138                                 //delete __THREADS__[threadId]; 
     152                                //delete __this__.__THREADS__[threadId]; 
    139153                        } 
    140154                }; 
     
    144158                        return {'error':'#0 - invalid params'}; 
    145159 
    146                 if ( __THREADS__[arguments[0]] ) 
     160                if ( __this__.__THREADS__[arguments[0]] ) 
    147161                        return {'error':'#2 - request is running'}; 
    148162 
    149                 __THREADS__[arguments[0]] = { 
     163                __this__.__THREADS__[arguments[0]] = { 
    150164                        '__HANDLER__' : arguments[1], 
    151165                        '__METHOD__'  : 'GET',  // __METHOD__ = [GET | POST] 
     
    154168                }; 
    155169 
    156                 makeConnection(); 
     170                makeConnection(arguments[0]); 
    157171 
    158172                if ( !verifyUserHandler(arguments[0], arguments[1]) ) 
    159173                { 
    160                         delete __THREADS__[arguments[0]]; 
     174                        //delete __this__.__THREADS__[arguments[0]]; 
    161175                        return {'error':'#1 - invalid handler'}; 
    162176                } 
     
    165179                        setPost(arguments[0]); 
    166180 
    167                 if ( __HTTP__ != null ) 
     181                if ( __this__.__HTTP__[arguments[0]] != null ) 
    168182                { 
    169183                        try 
    170184                        { 
    171185                                var __TARGET__ = path_im + 'controller.php?act=' + arguments[0]; 
     186 
     187                                //alert("__METHOD__: " + __this__.__THREADS__[arguments[0]].__METHOD__ + "\n__TARGET__: " + __TARGET__) 
     188 
    172189                                var __SEND__ = null; 
    173                                 __HTTP__.open(__THREADS__[arguments[0]].__METHOD__, __TARGET__, true); 
    174                                 if ( __THREADS__[arguments[0]].__METHOD__ == 'POST' ) 
    175                                 { 
    176                                         __HTTP__.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
    177                                         __HTTP__.setRequestHeader('Cache-Control',  'no-store, no-cache, must-revalidate'); 
    178                                         __HTTP__.setRequestHeader('Cache-Control', 'post-check=0, pre-check=0'); 
    179                                         __HTTP__.setRequestHeader('Pragma', 'no-cache'); 
     190                                __this__.__HTTP__[arguments[0]].open(__this__.__THREADS__[arguments[0]].__METHOD__, __TARGET__, true); 
     191                                if ( __this__.__THREADS__[arguments[0]].__METHOD__ == 'POST' ) 
     192                                { 
     193                                        __this__.__HTTP__[arguments[0]].setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
     194                                        __this__.__HTTP__[arguments[0]].setRequestHeader('Cache-Control',  'no-store, no-cache, must-revalidate'); 
     195                                        __this__.__HTTP__[arguments[0]].setRequestHeader('Cache-Control', 'post-check=0, pre-check=0'); 
     196                                        __this__.__HTTP__[arguments[0]].setRequestHeader('Pragma', 'no-cache'); 
    180197                                        __SEND__ = arguments[2]; 
    181198                                } 
    182                                 //__HTTP__.setRequestHeader('ThreadId', arguments[0]); 
    183                                 //__HTTP__.setRequestHeader('Connection', 'close'); 
    184                                 //__HTTP__.setRequestHeader('Keep-Alive', '0'); 
    185                                 __HTTP__.onreadystatechange = __HANDLER__; 
    186                                 __HTTP__.send(__SEND__); 
     199                                //__this__.__HTTP__.setRequestHeader('ThreadId', arguments[0]); 
     200                                //__this__.__HTTP__.setRequestHeader('Connection', 'close'); 
     201                                //__this__.__HTTP__.setRequestHeader('Keep-Alive', '0'); 
     202                                var tst = arguments[0]; 
     203                                __this__.__HTTP__[arguments[0]].onreadystatechange = function(){__HANDLER__(tst)}; 
     204                                __this__.__HTTP__[arguments[0]].send(__SEND__); 
    187205                        } 
    188206                        catch(e) 
Note: See TracChangeset for help on using the changeset viewer.