Skip to content

Commit

Permalink
Improved Tcp & WebSocket Client
Browse files Browse the repository at this point in the history
  • Loading branch information
andot committed Jul 13, 2016
1 parent e4530a7 commit be3101e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 15 additions & 11 deletions src/TcpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* *
* hprose tcp client for HTML5. *
* *
* LastModified: Mar 2, 2016 *
* LastModified: Jul 14, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
Expand Down Expand Up @@ -142,7 +142,7 @@
fetch: { value: function() {
var pool = this.pool;
while (pool.length > 0) {
var conn = pool.shift();
var conn = pool.pop();
if (conn.connected) {
if (conn.count === 0) {
conn.clearTimeout();
Expand Down Expand Up @@ -195,12 +195,14 @@
sendNext: { value: function(conn) {
if (conn.count < 10) {
if (this.requests.length > 0) {
var request = this.requests.shift();
var request = this.requests.pop();
request.push(conn);
this.send.apply(this, request);
}
else {
this.pool.push(conn);
if (this.pool.lastIndexOf(conn) < 0) {
this.pool.push(conn);
}
}
}
} },
Expand Down Expand Up @@ -265,7 +267,7 @@
fetch: { value: function() {
var pool = this.pool;
while (pool.length > 0) {
var conn = pool.shift();
var conn = pool.pop();
if (conn.connected) {
conn.clearTimeout();
conn.ref();
Expand All @@ -275,11 +277,13 @@
return null;
} },
recycle: { value: function(conn) {
conn.unref();
conn.setTimeout(this.client.poolTimeout, function() {
conn.destroy();
});
this.pool.push(conn);
if (this.pool.lastIndexOf(conn) < 0) {
conn.unref();
conn.setTimeout(this.client.poolTimeout, function() {
conn.destroy();
});
this.pool.push(conn);
}
} },
clean: { value: function(conn) {
conn.onreceive = noop;
Expand All @@ -291,7 +295,7 @@
} },
sendNext: { value: function(conn) {
if (this.requests.length > 0) {
var request = this.requests.shift();
var request = this.requests.pop();
request.push(conn);
this.send.apply(this, request);
}
Expand Down
4 changes: 2 additions & 2 deletions src/WebSocketClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* *
* hprose websocket client for HTML5. *
* *
* LastModified: Mar 2, 2016 *
* LastModified: Jul 14, 2016 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
Expand Down Expand Up @@ -84,7 +84,7 @@
}
if ((_count < 100) && (_requests.length > 0)) {
++_count;
var request = _requests.shift();
var request = _requests.pop();
_ready.then(function() { send(request[0], request[1]); });
}
if (_count === 0 && !self.keepAlive) {
Expand Down

0 comments on commit be3101e

Please sign in to comment.