From be3101e3f842c8b25276e6ff48687c874b6fbae1 Mon Sep 17 00:00:00 2001 From: Ma Bingyao Date: Thu, 14 Jul 2016 01:06:08 +0800 Subject: [PATCH] Improved Tcp & WebSocket Client --- src/TcpClient.js | 26 +++++++++++++++----------- src/WebSocketClient.js | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/TcpClient.js b/src/TcpClient.js index 85c5cf9..260b315 100644 --- a/src/TcpClient.js +++ b/src/TcpClient.js @@ -12,7 +12,7 @@ * * * hprose tcp client for HTML5. * * * - * LastModified: Mar 2, 2016 * + * LastModified: Jul 14, 2016 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -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(); @@ -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); + } } } } }, @@ -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(); @@ -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; @@ -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); } diff --git a/src/WebSocketClient.js b/src/WebSocketClient.js index 54b76c0..82a3965 100644 --- a/src/WebSocketClient.js +++ b/src/WebSocketClient.js @@ -12,7 +12,7 @@ * * * hprose websocket client for HTML5. * * * - * LastModified: Mar 2, 2016 * + * LastModified: Jul 14, 2016 * * Author: Ma Bingyao * * * \**********************************************************/ @@ -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) {