1
1
/*
2
2
* Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles)
3
3
* Copyright (c) 2010 Strumsoft (https://strumsoft.com)
4
- *
4
+ *
5
5
* Permission is hereby granted, free of charge, to any person
6
6
* obtaining a copy of this software and associated documentation
7
7
* files (the "Software"), to deal in the Software without
10
10
* copies of the Software, and to permit persons to whom the
11
11
* Software is furnished to do so, subject to the following
12
12
* conditions:
13
- *
13
+ *
14
14
* The above copyright notice and this permission notice shall be
15
15
* included in all copies or substantial portions of the Software.
16
- *
16
+ *
17
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
18
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
19
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
22
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
23
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
24
* OTHER DEALINGS IN THE SOFTWARE.
25
- *
25
+ *
26
26
*/
27
27
( function ( ) {
28
-
28
+
29
29
// window object
30
30
var global = window ;
31
-
31
+
32
32
// WebSocket Object. All listener methods are cleaned up!
33
33
var WebSocket = global . WebSocket = function ( url ) {
34
34
// get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
40
40
throw new Error ( 'Websocket instantiation failed! Address might be wrong.' ) ;
41
41
}
42
42
} ;
43
-
43
+
44
+ // private property
45
+ WebSocket . _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ;
46
+
47
+ // private method for decoding base64
48
+ WebSocket . _decode = function ( input ) {
49
+ var output = "" ;
50
+ var chr1 , chr2 , chr3 ;
51
+ var enc1 , enc2 , enc3 , enc4 ;
52
+ var i = 0 ;
53
+
54
+ input = input . replace ( / [ ^ A - Z a - z 0 - 9 \+ \/ \= ] / g, "" ) ;
55
+
56
+ while ( i < input . length ) {
57
+
58
+ enc1 = this . _keyStr . indexOf ( input . charAt ( i ++ ) ) ;
59
+ enc2 = this . _keyStr . indexOf ( input . charAt ( i ++ ) ) ;
60
+ enc3 = this . _keyStr . indexOf ( input . charAt ( i ++ ) ) ;
61
+ enc4 = this . _keyStr . indexOf ( input . charAt ( i ++ ) ) ;
62
+
63
+ chr1 = ( enc1 << 2 ) | ( enc2 >> 4 ) ;
64
+ chr2 = ( ( enc2 & 15 ) << 4 ) | ( enc3 >> 2 ) ;
65
+ chr3 = ( ( enc3 & 3 ) << 6 ) | enc4 ;
66
+
67
+ output = output + String . fromCharCode ( chr1 ) ;
68
+
69
+ if ( enc3 != 64 ) {
70
+ output = output + String . fromCharCode ( chr2 ) ;
71
+ }
72
+ if ( enc4 != 64 ) {
73
+ output = output + String . fromCharCode ( chr3 ) ;
74
+ }
75
+
76
+ }
77
+
78
+ output = this . _utf8_decode ( output ) ;
79
+
80
+ return output ;
81
+
82
+ }
83
+
84
+ // private method for UTF-8 decoding
85
+ WebSocket . _utf8_decode = function ( utftext ) {
86
+ var string = "" ;
87
+ var i = 0 ;
88
+ var c = c1 = c2 = 0 ;
89
+
90
+ while ( i < utftext . length ) {
91
+
92
+ c = utftext . charCodeAt ( i ) ;
93
+
94
+ if ( c < 128 ) {
95
+ string += String . fromCharCode ( c ) ;
96
+ i ++ ;
97
+ }
98
+ else if ( ( c > 191 ) && ( c < 224 ) ) {
99
+ c2 = utftext . charCodeAt ( i + 1 ) ;
100
+ string += String . fromCharCode ( ( ( c & 31 ) << 6 ) | ( c2 & 63 ) ) ;
101
+ i += 2 ;
102
+ }
103
+ else {
104
+ c2 = utftext . charCodeAt ( i + 1 ) ;
105
+ c3 = utftext . charCodeAt ( i + 2 ) ;
106
+ string += String . fromCharCode ( ( ( c & 15 ) << 12 ) | ( ( c2 & 63 ) << 6 ) | ( c3 & 63 ) ) ;
107
+ i += 3 ;
108
+ }
109
+
110
+ }
111
+
112
+ return string ;
113
+ }
114
+
44
115
// storage to hold websocket object for later invokation of event methods
45
116
WebSocket . store = { } ;
46
-
117
+
47
118
// static event methods to call event methods on target websocket objects
48
119
WebSocket . onmessage = function ( evt ) {
49
- WebSocket . store [ evt . _target ] [ 'onmessage' ] . call ( global , evt ) ;
50
- }
51
-
120
+ WebSocket . store [ evt . _target ] [ 'onmessage' ] . call ( global , this . _decode ( evt . _data ) ) ;
121
+ }
122
+
52
123
WebSocket . onopen = function ( evt ) {
53
124
WebSocket . store [ evt . _target ] [ 'onopen' ] . call ( global , evt ) ;
54
125
}
55
-
126
+
56
127
WebSocket . onclose = function ( evt ) {
57
128
WebSocket . store [ evt . _target ] [ 'onclose' ] . call ( global , evt ) ;
58
129
}
59
-
130
+
60
131
WebSocket . onerror = function ( evt ) {
61
132
WebSocket . store [ evt . _target ] [ 'onerror' ] . call ( global , evt ) ;
62
133
}
69
140
WebSocket . prototype . close = function ( ) {
70
141
this . socket . close ( ) ;
71
142
}
72
-
143
+
73
144
WebSocket . prototype . getReadyState = function ( ) {
74
145
this . socket . getReadyState ( ) ;
75
146
}
76
147
///////////// Must be overloaded
77
148
WebSocket . prototype . onopen = function ( ) {
78
149
throw new Error ( 'onopen not implemented.' ) ;
79
150
} ;
80
-
151
+
81
152
// alerts message pushed from server
82
153
WebSocket . prototype . onmessage = function ( msg ) {
83
154
throw new Error ( 'onmessage not implemented.' ) ;
84
155
} ;
85
-
156
+
86
157
// alerts message pushed from server
87
158
WebSocket . prototype . onerror = function ( msg ) {
88
159
throw new Error ( 'onerror not implemented.' ) ;
89
160
} ;
90
-
161
+
91
162
// alert close event
92
163
WebSocket . prototype . onclose = function ( ) {
93
164
throw new Error ( 'onclose not implemented.' ) ;
94
165
} ;
95
- } ) ( ) ;
166
+ } ) ( ) ;
0 commit comments