[151] | 1 | function Connector() |
---|
| 2 | { |
---|
[235] | 3 | this.__HTTP__ = []; |
---|
| 4 | this.__THREADS__ = []; |
---|
[151] | 5 | } |
---|
| 6 | |
---|
| 7 | Connector.prototype = { |
---|
| 8 | 'go' : function() |
---|
| 9 | { |
---|
| 10 | // connection |
---|
[235] | 11 | //var __HTTP__ = null; |
---|
| 12 | //var __THREADS__ = []; |
---|
[151] | 13 | var index = 0; |
---|
| 14 | var rtlen = 0; |
---|
| 15 | var ie = false; // ie don't has stream |
---|
[235] | 16 | var __this__ = this; |
---|
| 17 | function makeConnection(pThread) |
---|
[151] | 18 | { |
---|
[235] | 19 | //if ( __this__.__HTTP__ === false ) |
---|
| 20 | __this__.__HTTP__[pThread] = xhr(); |
---|
[151] | 21 | } |
---|
| 22 | function setPost(pThread) |
---|
| 23 | { |
---|
[235] | 24 | __this__.__THREADS__[pThread].__METHOD__ = 'POST'; |
---|
[151] | 25 | return true; |
---|
| 26 | } |
---|
[235] | 27 | // this set __this__.__THREADS__[?].__TYPE__ to 4 for request or 3 for stream |
---|
[151] | 28 | function setStream(pThread) |
---|
| 29 | { |
---|
[235] | 30 | __this__.__THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4; |
---|
[151] | 31 | return true; |
---|
| 32 | } |
---|
| 33 | function trim(pData) |
---|
| 34 | { |
---|
| 35 | return pData.replace(/^ +| +$/g, ''); |
---|
| 36 | } |
---|
| 37 | function verifyUserHandler(pThread, pHandler) |
---|
| 38 | { |
---|
| 39 | var type_of_handler = typeof pHandler; |
---|
| 40 | var i; |
---|
| 41 | switch ( type_of_handler ) |
---|
| 42 | { |
---|
| 43 | case 'function' : |
---|
| 44 | return true; |
---|
| 45 | break; |
---|
| 46 | case 'object' : |
---|
| 47 | for ( i in pHandler ) |
---|
| 48 | if ( i != 'stream' && i != 'request' ) |
---|
| 49 | return false; |
---|
| 50 | else if ( i == 'stream' ) |
---|
| 51 | setStream(pThread) |
---|
| 52 | return true; |
---|
| 53 | break; |
---|
| 54 | default : |
---|
| 55 | return false; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | // xhr = XMLHttpRequest |
---|
| 59 | function xhr() |
---|
| 60 | { |
---|
| 61 | try |
---|
| 62 | { |
---|
| 63 | return new XMLHttpRequest(); |
---|
| 64 | } |
---|
| 65 | catch (e) |
---|
| 66 | { |
---|
| 67 | ie = true; |
---|
| 68 | try |
---|
| 69 | { |
---|
| 70 | return new ActiveXObject('Msxml2.XMLHTTP'); |
---|
| 71 | } |
---|
| 72 | catch (e1) |
---|
| 73 | { |
---|
| 74 | try |
---|
| 75 | { |
---|
| 76 | return new ActiveXObject('Microsoft.XMLHTTP'); |
---|
| 77 | } |
---|
| 78 | catch (e2) |
---|
| 79 | { |
---|
| 80 | return null; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
[235] | 86 | function __HANDLER__() |
---|
[151] | 87 | { |
---|
| 88 | try |
---|
| 89 | { |
---|
[235] | 90 | if ( ie && __this__.__HTTP__[arguments[0]].readyState != 4 ) |
---|
[151] | 91 | return false; |
---|
| 92 | |
---|
[235] | 93 | //var threadId = __this__.__HTTP__[arguments[0]].getResponseHeader('ThreadId'); |
---|
| 94 | var threadId = arguments[0]; |
---|
| 95 | |
---|
[151] | 96 | if ( !threadId ) |
---|
| 97 | return false; |
---|
| 98 | |
---|
[235] | 99 | if ( __this__.__HTTP__[threadId].readyState == 3 || __this__.__HTTP__[threadId].readyState == 4 ) |
---|
[151] | 100 | { |
---|
[235] | 101 | if ( !ie && __this__.__HTTP__[threadId].readyState == 3 && __this__.__THREADS__[threadId].__HANDLER__.stream ) |
---|
[151] | 102 | { |
---|
[235] | 103 | var data = trim(__this__.__HTTP__[threadId].responseText.substr(index)); |
---|
| 104 | rtlen = __this__.__HTTP__[threadId].responseText.length; |
---|
[151] | 105 | if ( index < rtlen && data.length ) |
---|
| 106 | { |
---|
[235] | 107 | __this__.__THREADS__[threadId].__HANDLER__.stream(data); |
---|
[151] | 108 | index = rtlen; |
---|
| 109 | } |
---|
| 110 | } |
---|
[235] | 111 | if ( __this__.__THREADS__[threadId].__HANDLER__.request && __this__.__HTTP__[threadId].readyState == 4 ) |
---|
[151] | 112 | { |
---|
[235] | 113 | switch ( __this__.__HTTP__[threadId].status ) |
---|
[151] | 114 | { |
---|
| 115 | case 200: |
---|
[235] | 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; |
---|
[151] | 121 | |
---|
[235] | 122 | if ( ie && __this__.__THREADS__[threadId].__HANDLER__.stream ) |
---|
| 123 | stream = __this__.__THREADS__[threadId].__HANDLER__.stream; |
---|
[151] | 124 | |
---|
[235] | 125 | if ( __this__.__THREADS__[threadId].__HANDLER__.request ) |
---|
| 126 | request = __this__.__THREADS__[threadId].__HANDLER__.request; |
---|
[151] | 127 | |
---|
[235] | 128 | delete __this__.__THREADS__[threadId]; |
---|
| 129 | |
---|
| 130 | if ( stream ) |
---|
| 131 | stream(data); |
---|
| 132 | |
---|
| 133 | if ( request ) |
---|
| 134 | request(data); |
---|
| 135 | |
---|
[151] | 136 | break; // [case : status 200] |
---|
| 137 | case 404: |
---|
| 138 | alert('Page Not Found!'); |
---|
| 139 | break; // [case : status 404] |
---|
| 140 | default: |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | } |
---|
[235] | 144 | //if ( __this__.__HTTP__.readyState == 4 ) |
---|
| 145 | //{ |
---|
| 146 | // delete __this__.__THREADS__[threadId]; |
---|
| 147 | //} |
---|
[151] | 148 | } |
---|
| 149 | catch(e) |
---|
| 150 | { |
---|
| 151 | //alert("__HANDLER__\n" + threadId + "\n " + e + "\n ErroR: " + e.description); |
---|
[235] | 152 | //delete __this__.__THREADS__[threadId]; |
---|
[151] | 153 | } |
---|
| 154 | }; |
---|
| 155 | |
---|
| 156 | var argc = arguments.length; |
---|
| 157 | if ( argc < 2 || argc > 4 ) |
---|
| 158 | return {'error':'#0 - invalid params'}; |
---|
| 159 | |
---|
[235] | 160 | if ( __this__.__THREADS__[arguments[0]] ) |
---|
[151] | 161 | return {'error':'#2 - request is running'}; |
---|
| 162 | |
---|
[235] | 163 | __this__.__THREADS__[arguments[0]] = { |
---|
[151] | 164 | '__HANDLER__' : arguments[1], |
---|
| 165 | '__METHOD__' : 'GET', // __METHOD__ = [GET | POST] |
---|
| 166 | '__MODE__' : 'TEXT', // __MODE__ = [XML | TEXT] |
---|
| 167 | '__TYPE__' : 4 // __TYPE__ = [4 for request | 3 for stream] |
---|
| 168 | }; |
---|
| 169 | |
---|
[235] | 170 | makeConnection(arguments[0]); |
---|
[151] | 171 | |
---|
| 172 | if ( !verifyUserHandler(arguments[0], arguments[1]) ) |
---|
| 173 | { |
---|
[235] | 174 | //delete __this__.__THREADS__[arguments[0]]; |
---|
[151] | 175 | return {'error':'#1 - invalid handler'}; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if ( argc == 3 ) |
---|
| 179 | setPost(arguments[0]); |
---|
| 180 | |
---|
[235] | 181 | if ( __this__.__HTTP__[arguments[0]] != null ) |
---|
[151] | 182 | { |
---|
| 183 | try |
---|
| 184 | { |
---|
| 185 | var __TARGET__ = path_im + 'controller.php?act=' + arguments[0]; |
---|
[235] | 186 | |
---|
| 187 | //alert("__METHOD__: " + __this__.__THREADS__[arguments[0]].__METHOD__ + "\n__TARGET__: " + __TARGET__) |
---|
| 188 | |
---|
[151] | 189 | var __SEND__ = null; |
---|
[235] | 190 | __this__.__HTTP__[arguments[0]].open(__this__.__THREADS__[arguments[0]].__METHOD__, __TARGET__, true); |
---|
| 191 | if ( __this__.__THREADS__[arguments[0]].__METHOD__ == 'POST' ) |
---|
[151] | 192 | { |
---|
[235] | 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'); |
---|
[151] | 197 | __SEND__ = arguments[2]; |
---|
| 198 | } |
---|
[235] | 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__); |
---|
[151] | 205 | } |
---|
| 206 | catch(e) |
---|
| 207 | { |
---|
| 208 | alert("go\n" + e.description) |
---|
| 209 | return {'error':'#3 - ' + e.description}; |
---|
| 210 | } |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | }; |
---|