Skip to content

Commit ee2359b

Browse files
committed
Added Hprose WebSocket Client.
1 parent ed6dacb commit ee2359b

File tree

9 files changed

+239
-20
lines changed

9 files changed

+239
-20
lines changed

dist/hprose-html5.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* *
1313
* hprose client for HTML5. *
1414
* *
15-
* LastModified: Mar 15, 2015 *
15+
* LastModified: Apr 17, 2015 *
1616
* Author: Ma Bingyao <[email protected]> *
1717
* *
1818
\**********************************************************/
@@ -708,6 +708,10 @@
708708
parser.protocol === 'https:') {
709709
return new global.hprose.HttpClient(uri, functions);
710710
}
711+
if (parser.protocol === 'ws:' ||
712+
parser.protocol === 'wss:') {
713+
return new global.hprose.WebSocketClient(uri, functions);
714+
}
711715
throw new Exception('The ' + parser.protocol + ' client isn\'t implemented.');
712716
}
713717

src/HttpClient.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,16 @@
1212
* *
1313
* hprose http client for HTML5. *
1414
* *
15-
* LastModified: Mar 15, 2015 *
15+
* LastModified: Apr 17, 2015 *
1616
* Author: Ma Bingyao <[email protected]> *
1717
* *
1818
\**********************************************************/
1919

2020
(function (global) {
2121
'use strict';
2222

23-
var Tags = global.hprose.Tags;
2423
var Exception = global.hprose.Exception;
2524
var Client = global.hprose.Client;
26-
var BytesIO = global.hprose.BytesIO;
27-
var serialize = global.hprose.serialize;
2825
var Completer = global.hprose.Completer;
2926

3027
function noop(){}
@@ -48,7 +45,7 @@
4845
for (var name in _header) {
4946
xhr.setRequestHeader(name, _header[name]);
5047
}
51-
var timeoutId;
48+
var timeoutId = undefined;
5249
xhr.onload = function () {
5350
xhr.onload = function() {};
5451
if (xhr.status) {
@@ -71,12 +68,14 @@
7168
}
7269
completer.completeError(new Exception('error'));
7370
};
74-
timeoutId = global.setTimeout(function () {
75-
xhr.onload = function() {};
76-
xhr.onerror = function() {};
77-
xhr.abort();
78-
completer.completeError(new Exception('timeout'));
79-
}, _timeout);
71+
if (_timeout > 0) {
72+
timeoutId = global.setTimeout(function () {
73+
xhr.onload = function() {};
74+
xhr.onerror = function() {};
75+
xhr.abort();
76+
completer.completeError(new Exception('timeout'));
77+
}, _timeout);
78+
}
8079
if (xhr.upload !== undefined) {
8180
xhr.upload.onprogress = _onreqprogress;
8281
}

src/WebSocketClient.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/**********************************************************\
2+
| |
3+
| hprose |
4+
| |
5+
| Official WebSite: http://www.hprose.com/ |
6+
| http://www.hprose.org/ |
7+
| |
8+
\**********************************************************/
9+
/**********************************************************\
10+
* *
11+
* WebSocketClient.js *
12+
* *
13+
* hprose websocket client for HTML5. *
14+
* *
15+
* LastModified: Apr 17, 2015 *
16+
* Author: Ma Bingyao <[email protected]> *
17+
* *
18+
\**********************************************************/
19+
20+
(function (global) {
21+
'use strict';
22+
23+
var Exception = global.hprose.Exception;
24+
var Client = global.hprose.Client;
25+
var BytesIO = global.hprose.BytesIO;
26+
var Completer = global.hprose.Completer;
27+
28+
function noop(){}
29+
var s_id = 0;
30+
var s_completers = [];
31+
var s_timeoutId = [];
32+
var s_messages = [];
33+
34+
global.hprose.WebSocketClient = function WebSocketClient(uri, functions) {
35+
Client.call(this, uri, functions);
36+
var _timeout = 0;
37+
var self = this;
38+
var ready = false;
39+
var ws;
40+
function send_message(id, request) {
41+
var bytes = new BytesIO();
42+
bytes.writeByte((id >> 24) & 0xff);
43+
bytes.writeByte((id >> 16) & 0xff);
44+
bytes.writeByte((id >> 8) & 0xff);
45+
bytes.writeByte(id & 0xff);
46+
if (request.constructor === String) {
47+
bytes.writeString(request);
48+
}
49+
else {
50+
bytes.write(request);
51+
}
52+
var message = bytes.bytes;
53+
if (ArrayBuffer.isView) {
54+
ws.send(message);
55+
}
56+
else if (message.buffer.slice) {
57+
ws.send(message.buffer.slice(0, message.length));
58+
}
59+
else {
60+
ws.send(message.buffer);
61+
}
62+
}
63+
function onopen(e) {
64+
ready = true;
65+
if (s_messages.length > 0) {
66+
for (var id in s_messages) {
67+
send_message(id, s_messages[id]);
68+
}
69+
s_messages = [];
70+
}
71+
}
72+
function onmessage(e) {
73+
var bytes = new BytesIO(e.data);
74+
var id = bytes.readByte() << 24;
75+
id = id | bytes.readByte() << 16;
76+
id = id | bytes.readByte() << 8;
77+
id = id | bytes.readByte();
78+
var timeoutId = s_timeoutId[id];
79+
var completer = s_completers[id];
80+
delete s_timeoutId[id];
81+
delete s_completers[id];
82+
if (timeoutId !== undefined) {
83+
global.clearTimeout(timeoutId);
84+
timeoutId = undefined;
85+
}
86+
completer.complete(bytes.read(bytes.length - 4));
87+
}
88+
function onclose(e) {
89+
connect();
90+
}
91+
function onerror(e) {
92+
self.onerror("WebSocket", new Exception(e.data));
93+
}
94+
function connect() {
95+
ready = false;
96+
ws = new WebSocket(self.uri);
97+
ws.binaryType = "arraybuffer";
98+
ws.onopen = onopen;
99+
ws.onmessage = onmessage;
100+
ws.onerror = onerror;
101+
ws.onclose = onclose;
102+
}
103+
function send(request) {
104+
var completer = new Completer();
105+
var timeoutId = undefined;
106+
if (_timeout > 0) {
107+
timeoutId = global.setTimeout((function (id) {
108+
return function() {
109+
delete s_completers[id];
110+
delete s_timeoutId[id];
111+
delete s_messages[id];
112+
ws.close();
113+
completer.completeError(new Exception('timeout'));
114+
}
115+
})(s_id), _timeout);
116+
}
117+
s_completers[s_id] = completer;
118+
s_timeoutId[s_id] = timeoutId;
119+
if (ready) {
120+
send_message(s_id, request);
121+
}
122+
else {
123+
s_messages[s_id] = request;
124+
}
125+
if (s_id < 0x7fffffff) {
126+
++s_id;
127+
}
128+
else {
129+
s_id = 0;
130+
}
131+
return completer.future;
132+
}
133+
function setTimeout(value) {
134+
if (typeof(value) === 'number') {
135+
_timeout = value | 0;
136+
}
137+
else {
138+
_timeout = 0;
139+
}
140+
}
141+
function getTimeout() {
142+
return _timeout;
143+
}
144+
Object.defineProperties(this, {
145+
timeout: { get: getTimeout, set: setTimeout },
146+
__send__: { value: send }
147+
});
148+
connect();
149+
};
150+
151+
function create(uri, functions) {
152+
var parser = document.createElement('a');
153+
parser.href = uri;
154+
if (parser.protocol === 'ws:' ||
155+
parser.protocol === 'wss:') {
156+
return new global.hprose.WebSocketClient(uri, functions);
157+
}
158+
throw new Exception('This client desn\'t support ' + parser.protocol + ' scheme.');
159+
}
160+
161+
Object.defineProperty(global.hprose.WebSocketClient, 'create', { value: create });
162+
})(this);

test/Client.Test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
/**********************************************************\
1111
* *
12-
* Writer.Test.js *
12+
* Client.Test.js *
1313
* *
14-
* hprose Writer test for HTML5. *
14+
* hprose Client test for HTML5. *
1515
* *
16-
* LastModified: Mar 15, 2015 *
16+
* LastModified: Apr 17, 2015 *
1717
* Author: Ma Bingyao <[email protected]> *
1818
* *
1919
\**********************************************************/

test/Reader.Test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
/**********************************************************\
1111
* *
12-
* Writer.Test.js *
12+
* Reader.Test.js *
1313
* *
14-
* hprose Writer test for HTML5. *
14+
* hprose Reader test for HTML5. *
1515
* *
16-
* LastModified: Mar 28, 2014 *
16+
* LastModified: Apr 17, 2014 *
1717
* Author: Ma Bingyao <[email protected]> *
1818
* *
1919
\**********************************************************/

test/WebSocketClient.Test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**********************************************************\
2+
| |
3+
| hprose |
4+
| |
5+
| Official WebSite: http://www.hprose.com/ |
6+
| http://www.hprose.org/ |
7+
| |
8+
\**********************************************************/
9+
10+
/**********************************************************\
11+
* *
12+
* WebSocketClient.Test.js *
13+
* *
14+
* hprose websocket client test for HTML5. *
15+
* *
16+
* LastModified: Apr 17, 2015 *
17+
* Author: Ma Bingyao <[email protected]> *
18+
* *
19+
\**********************************************************/
20+
21+
/*global hprose */
22+
/*jshint eqeqeq:true, devel:true */
23+
24+
(function() {
25+
'use strict';
26+
var client = new hprose.Client.create('ws://127.0.0.1:8080');
27+
client.then(function(stub) {
28+
stub.hello('World')
29+
.then(function(result) {
30+
console.info(result);
31+
});
32+
client.beginBatch();
33+
stub.hello('World 1')
34+
.then(function(result) {
35+
console.info(result);
36+
});
37+
stub.hello('World 2')
38+
.then(function(result) {
39+
console.info(result);
40+
});
41+
stub.hello('World 3')
42+
.then(function(result) {
43+
console.info(result);
44+
});
45+
client.endBatch();
46+
})
47+
.catchError(function(e) {
48+
console.error(e);
49+
});
50+
51+
})();

test/test_dist.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<script type="text/javascript" src="Writer.Test.js"></script>
1010
<script type="text/javascript" src="Reader.Test.js"></script>
1111
<script type="text/javascript" src="Client.Test.js"></script>
12+
<script type="text/javascript" src="WebSocketClient.Test.js"></script>
1213
</head>
1314
<body>
1415

test/test_src.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
<script type="text/javascript" src="../src/Formatter.js"></script>
1616
<script type="text/javascript" src="../src/Client.js"></script>
1717
<script type="text/javascript" src="../src/HttpClient.js"></script>
18+
<script type="text/javascript" src="../src/WebSocketClient.js"></script>
1819
<script type="text/javascript" src="HarmonyMaps.Test.js"></script>
1920
<script type="text/javascript" src="BytesIO.Test.js"></script>
2021
<script type="text/javascript" src="Writer.Test.js"></script>
2122
<script type="text/javascript" src="Reader.Test.js"></script>
2223
<script type="text/javascript" src="Client.Test.js"></script>
24+
<script type="text/javascript" src="WebSocketClient.Test.js"></script>
2325
</head>
2426
<body>
2527

0 commit comments

Comments
 (0)