Changeset 235 for trunk/instant_messenger/js/connector.js
- Timestamp:
- 03/19/08 17:15:13 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/instant_messenger/js/connector.js
r151 r235 1 1 function Connector() 2 2 { 3 this.__HTTP__ = []; 4 this.__THREADS__ = []; 3 5 } 4 6 … … 7 9 { 8 10 // connection 9 var __HTTP__ = null;10 var __THREADS__ = [];11 //var __HTTP__ = null; 12 //var __THREADS__ = []; 11 13 var index = 0; 12 14 var rtlen = 0; 13 15 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(); 18 21 } 19 22 function setPost(pThread) 20 23 { 21 __ THREADS__[pThread].__METHOD__ = 'POST';24 __this__.__THREADS__[pThread].__METHOD__ = 'POST'; 22 25 return true; 23 26 } 24 // this set __ THREADS__[?].__TYPE__ to 4 for request or 3 for stream27 // this set __this__.__THREADS__[?].__TYPE__ to 4 for request or 3 for stream 25 28 function setStream(pThread) 26 29 { 27 __ THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4;30 __this__.__THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4; 28 31 return true; 29 32 } … … 81 84 } 82 85 83 var __HANDLER__ = function()86 function __HANDLER__() 84 87 { 85 88 try 86 89 { 87 if ( ie && __ HTTP__.readyState != 4 )90 if ( ie && __this__.__HTTP__[arguments[0]].readyState != 4 ) 88 91 return false; 89 92 90 var threadId = __HTTP__.getResponseHeader('ThreadId'); 93 //var threadId = __this__.__HTTP__[arguments[0]].getResponseHeader('ThreadId'); 94 var threadId = arguments[0]; 95 91 96 if ( !threadId ) 92 97 return false; 93 98 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; 101 105 if ( index < rtlen && data.length ) 102 106 { 103 __ THREADS__[threadId].__HANDLER__.stream(data);107 __this__.__THREADS__[threadId].__HANDLER__.stream(data); 104 108 index = rtlen; 105 109 } 106 110 } 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 ) 110 114 { 111 115 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); 121 135 122 136 break; // [case : status 200] … … 128 142 } 129 143 } 130 if (__HTTP__.readyState == 4 )131 {132 delete__THREADS__[threadId];133 }144 //if ( __this__.__HTTP__.readyState == 4 ) 145 //{ 146 // delete __this__.__THREADS__[threadId]; 147 //} 134 148 } 135 149 catch(e) 136 150 { 137 151 //alert("__HANDLER__\n" + threadId + "\n " + e + "\n ErroR: " + e.description); 138 //delete __ THREADS__[threadId];152 //delete __this__.__THREADS__[threadId]; 139 153 } 140 154 }; … … 144 158 return {'error':'#0 - invalid params'}; 145 159 146 if ( __ THREADS__[arguments[0]] )160 if ( __this__.__THREADS__[arguments[0]] ) 147 161 return {'error':'#2 - request is running'}; 148 162 149 __ THREADS__[arguments[0]] = {163 __this__.__THREADS__[arguments[0]] = { 150 164 '__HANDLER__' : arguments[1], 151 165 '__METHOD__' : 'GET', // __METHOD__ = [GET | POST] … … 154 168 }; 155 169 156 makeConnection( );170 makeConnection(arguments[0]); 157 171 158 172 if ( !verifyUserHandler(arguments[0], arguments[1]) ) 159 173 { 160 delete__THREADS__[arguments[0]];174 //delete __this__.__THREADS__[arguments[0]]; 161 175 return {'error':'#1 - invalid handler'}; 162 176 } … … 165 179 setPost(arguments[0]); 166 180 167 if ( __ HTTP__!= null )181 if ( __this__.__HTTP__[arguments[0]] != null ) 168 182 { 169 183 try 170 184 { 171 185 var __TARGET__ = path_im + 'controller.php?act=' + arguments[0]; 186 187 //alert("__METHOD__: " + __this__.__THREADS__[arguments[0]].__METHOD__ + "\n__TARGET__: " + __TARGET__) 188 172 189 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'); 180 197 __SEND__ = arguments[2]; 181 198 } 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__); 187 205 } 188 206 catch(e)
Note: See TracChangeset
for help on using the changeset viewer.