source: trunk/instant_messenger/js/connector.js @ 235

Revision 235, 5.6 KB checked in by niltonneto, 16 years ago (diff)
Line 
1function Connector()
2{
3        this.__HTTP__ = [];
4        this.__THREADS__ = [];
5}
6
7Connector.prototype = {
8        'go' : function()
9        {
10                // connection
11                //var __HTTP__ = null;
12                //var __THREADS__ = [];
13                var index = 0;
14                var rtlen = 0;
15                var ie = false; // ie don't has stream
16                var __this__ = this;
17                function makeConnection(pThread)
18                {
19                        //if ( __this__.__HTTP__ === false )
20                                __this__.__HTTP__[pThread] = xhr();
21                }
22                function setPost(pThread)
23                {
24                        __this__.__THREADS__[pThread].__METHOD__ = 'POST';
25                        return true;
26                }
27                // this set __this__.__THREADS__[?].__TYPE__ to 4 for request or 3 for stream
28                function setStream(pThread)
29                {
30                        __this__.__THREADS__[pThread].__TYPE__ = ( !ie ) ? 3 : 4;
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
86                function __HANDLER__()
87                {
88                        try
89                        {
90                                if ( ie && __this__.__HTTP__[arguments[0]].readyState != 4 )
91                                        return false;
92
93                                //var threadId = __this__.__HTTP__[arguments[0]].getResponseHeader('ThreadId');
94                                var threadId = arguments[0];
95
96                                if ( !threadId )
97                                        return false;
98
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;
105                                                if ( index < rtlen && data.length )
106                                                {
107                                                        __this__.__THREADS__[threadId].__HANDLER__.stream(data);
108                                                        index = rtlen;
109                                                }
110                                        }
111                                        if ( __this__.__THREADS__[threadId].__HANDLER__.request && __this__.__HTTP__[threadId].readyState == 4 )
112                                        {
113                                                switch ( __this__.__HTTP__[threadId].status )
114                                                {
115                                                        case 200:
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);
135
136                                                                break; // [case : status 200]
137                                                        case 404:
138                                                                alert('Page Not Found!');
139                                                                break; // [case : status 404]
140                                                        default:
141                                                }
142                                        }
143                                }
144                                //if ( __this__.__HTTP__.readyState == 4 )
145                                //{
146                                //      delete __this__.__THREADS__[threadId];
147                                //}
148                        }
149                        catch(e)
150                        {
151                                //alert("__HANDLER__\n" + threadId + "\n " + e + "\n ErroR: " + e.description);
152                                //delete __this__.__THREADS__[threadId];
153                        }
154                };
155
156                var argc = arguments.length;
157                if ( argc < 2 || argc > 4 )
158                        return {'error':'#0 - invalid params'};
159
160                if ( __this__.__THREADS__[arguments[0]] )
161                        return {'error':'#2 - request is running'};
162
163                __this__.__THREADS__[arguments[0]] = {
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
170                makeConnection(arguments[0]);
171
172                if ( !verifyUserHandler(arguments[0], arguments[1]) )
173                {
174                        //delete __this__.__THREADS__[arguments[0]];
175                        return {'error':'#1 - invalid handler'};
176                }
177
178                if ( argc == 3 )
179                        setPost(arguments[0]);
180
181                if ( __this__.__HTTP__[arguments[0]] != null )
182                {
183                        try
184                        {
185                                var __TARGET__ = path_im + 'controller.php?act=' + arguments[0];
186
187                                //alert("__METHOD__: " + __this__.__THREADS__[arguments[0]].__METHOD__ + "\n__TARGET__: " + __TARGET__)
188
189                                var __SEND__ = null;
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');
197                                        __SEND__ = arguments[2];
198                                }
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__);
205                        }
206                        catch(e)
207                        {
208                                alert("go\n" + e.description)
209                                return {'error':'#3 - ' + e.description};
210                        }
211                }
212        }
213};
Note: See TracBrowser for help on using the repository browser.