From 9b56db47763b134542dda646a77bb5597b33176d Mon Sep 17 00:00:00 2001 From: lesismal Date: Fri, 6 Oct 2023 14:56:32 +0800 Subject: [PATCH] fix js client ping/pong dead loop --- examples/httprpc/arpc.js | 9 ++++++--- examples/protocols/websocket/jsclient/arpc.js | 9 ++++++--- examples/webchat/arpc.js | 9 ++++++--- extension/jsclient/arpc.js | 9 ++++++--- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/examples/httprpc/arpc.js b/examples/httprpc/arpc.js index 638c709..e1c086b 100644 --- a/examples/httprpc/arpc.js +++ b/examples/httprpc/arpc.js @@ -149,7 +149,7 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { if (this._keepaliveInited) return; this._keepaliveInited = true; if (!timeout) timeout = 1000 * 30; - setInterval(this.ping, timeout); + this.keepaliveIntervalID = setInterval(this.ping, timeout); } this.write = function(cmd, method, arg, cb, isHttp) { @@ -190,6 +190,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { this.shutdown = function() { this.ws.close(); this.state = _SOCK_STATE_CLOSED; + if (!!this.keepaliveIntervalID) { + clearInterval(this.keepaliveIntervalID); + } } this.request = function(data, cb) { @@ -246,9 +249,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { switch (cmd) { case _CmdPing: client.pong(); - continue; + return; case _CmdPong: - continue; + return; } if (methodLen == 0) { diff --git a/examples/protocols/websocket/jsclient/arpc.js b/examples/protocols/websocket/jsclient/arpc.js index 638c709..e1c086b 100644 --- a/examples/protocols/websocket/jsclient/arpc.js +++ b/examples/protocols/websocket/jsclient/arpc.js @@ -149,7 +149,7 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { if (this._keepaliveInited) return; this._keepaliveInited = true; if (!timeout) timeout = 1000 * 30; - setInterval(this.ping, timeout); + this.keepaliveIntervalID = setInterval(this.ping, timeout); } this.write = function(cmd, method, arg, cb, isHttp) { @@ -190,6 +190,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { this.shutdown = function() { this.ws.close(); this.state = _SOCK_STATE_CLOSED; + if (!!this.keepaliveIntervalID) { + clearInterval(this.keepaliveIntervalID); + } } this.request = function(data, cb) { @@ -246,9 +249,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { switch (cmd) { case _CmdPing: client.pong(); - continue; + return; case _CmdPong: - continue; + return; } if (methodLen == 0) { diff --git a/examples/webchat/arpc.js b/examples/webchat/arpc.js index 638c709..e1c086b 100644 --- a/examples/webchat/arpc.js +++ b/examples/webchat/arpc.js @@ -149,7 +149,7 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { if (this._keepaliveInited) return; this._keepaliveInited = true; if (!timeout) timeout = 1000 * 30; - setInterval(this.ping, timeout); + this.keepaliveIntervalID = setInterval(this.ping, timeout); } this.write = function(cmd, method, arg, cb, isHttp) { @@ -190,6 +190,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { this.shutdown = function() { this.ws.close(); this.state = _SOCK_STATE_CLOSED; + if (!!this.keepaliveIntervalID) { + clearInterval(this.keepaliveIntervalID); + } } this.request = function(data, cb) { @@ -246,9 +249,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { switch (cmd) { case _CmdPing: client.pong(); - continue; + return; case _CmdPong: - continue; + return; } if (methodLen == 0) { diff --git a/extension/jsclient/arpc.js b/extension/jsclient/arpc.js index 638c709..e1c086b 100644 --- a/extension/jsclient/arpc.js +++ b/extension/jsclient/arpc.js @@ -149,7 +149,7 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { if (this._keepaliveInited) return; this._keepaliveInited = true; if (!timeout) timeout = 1000 * 30; - setInterval(this.ping, timeout); + this.keepaliveIntervalID = setInterval(this.ping, timeout); } this.write = function(cmd, method, arg, cb, isHttp) { @@ -190,6 +190,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { this.shutdown = function() { this.ws.close(); this.state = _SOCK_STATE_CLOSED; + if (!!this.keepaliveIntervalID) { + clearInterval(this.keepaliveIntervalID); + } } this.request = function(data, cb) { @@ -246,9 +249,9 @@ function ArpcClient(url, codec, httpUrl, httpMethod) { switch (cmd) { case _CmdPing: client.pong(); - continue; + return; case _CmdPong: - continue; + return; } if (methodLen == 0) {