Skip to content

Commit 3164075

Browse files
author
Animesh Kumar
committedSep 25, 2013
Merge pull request #21 from uken/perf-improvements
improve performance of WebSocket for large messages
2 parents 07db347 + 2b51db5 commit 3164075

File tree

6 files changed

+2319
-136
lines changed

6 files changed

+2319
-136
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.project
2+
bin
3+
gen

‎assets/www/js/websocket.js

+89-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles)
33
* Copyright (c) 2010 Strumsoft (https://strumsoft.com)
4-
*
4+
*
55
* Permission is hereby granted, free of charge, to any person
66
* obtaining a copy of this software and associated documentation
77
* files (the "Software"), to deal in the Software without
@@ -10,10 +10,10 @@
1010
* copies of the Software, and to permit persons to whom the
1111
* Software is furnished to do so, subject to the following
1212
* conditions:
13-
*
13+
*
1414
* The above copyright notice and this permission notice shall be
1515
* included in all copies or substantial portions of the Software.
16-
*
16+
*
1717
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1818
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1919
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -22,13 +22,13 @@
2222
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2323
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2424
* OTHER DEALINGS IN THE SOFTWARE.
25-
*
25+
*
2626
*/
2727
(function() {
28-
28+
2929
// window object
3030
var global = window;
31-
31+
3232
// WebSocket Object. All listener methods are cleaned up!
3333
var WebSocket = global.WebSocket = function(url) {
3434
// get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
@@ -40,23 +40,94 @@
4040
throw new Error('Websocket instantiation failed! Address might be wrong.');
4141
}
4242
};
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-Za-z0-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+
44115
// storage to hold websocket object for later invokation of event methods
45116
WebSocket.store = {};
46-
117+
47118
// static event methods to call event methods on target websocket objects
48119
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+
52123
WebSocket.onopen = function (evt) {
53124
WebSocket.store[evt._target]['onopen'].call(global, evt);
54125
}
55-
126+
56127
WebSocket.onclose = function (evt) {
57128
WebSocket.store[evt._target]['onclose'].call(global, evt);
58129
}
59-
130+
60131
WebSocket.onerror = function (evt) {
61132
WebSocket.store[evt._target]['onerror'].call(global, evt);
62133
}
@@ -69,27 +140,27 @@
69140
WebSocket.prototype.close = function() {
70141
this.socket.close();
71142
}
72-
143+
73144
WebSocket.prototype.getReadyState = function() {
74145
this.socket.getReadyState();
75146
}
76147
///////////// Must be overloaded
77148
WebSocket.prototype.onopen = function(){
78149
throw new Error('onopen not implemented.');
79150
};
80-
151+
81152
// alerts message pushed from server
82153
WebSocket.prototype.onmessage = function(msg){
83154
throw new Error('onmessage not implemented.');
84155
};
85-
156+
86157
// alerts message pushed from server
87158
WebSocket.prototype.onerror = function(msg){
88159
throw new Error('onerror not implemented.');
89160
};
90-
161+
91162
// alert close event
92163
WebSocket.prototype.onclose = function(){
93164
throw new Error('onclose not implemented.');
94165
};
95-
})();
166+
})();

‎src/com/strumsoft/App.java

-46
This file was deleted.

0 commit comments

Comments
 (0)
Failed to load comments.