diff --git a/package.json b/package.json index 6b6da70..3047c5d 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "oauth": "^0.9.15", "socks-proxy-agent": "h3poteto/node-socks-proxy-agent#master", "typescript": "3.5.3", - "ws": "^7.0.1" + "ws": "^7.2.1" }, "devDependencies": { "@types/core-js": "^2.5.0", diff --git a/src/web_socket.ts b/src/web_socket.ts index af2723b..a0bc17e 100644 --- a/src/web_socket.ts +++ b/src/web_socket.ts @@ -44,7 +44,7 @@ export default class WebSocket extends EventEmitter { } this.proxyConfig = proxyConfig this._accessToken = accessToken - this._reconnectInterval = 1000 + this._reconnectInterval = 10000 this._reconnectMaxAttempts = Infinity this._reconnectCurrentAttempts = 0 this._connectionClosed = false @@ -106,23 +106,27 @@ export default class WebSocket extends EventEmitter { * Reconnects to the same endpoint. */ private _reconnect() { - if (this._client) { - setTimeout(() => { - if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) { - this._reconnectCurrentAttempts++ - this._clearBinding() - if (this._client) { - // In reconnect, we want to close the connection immediately, - // because recoonect is necessary when some problems occur. - this._client.terminate() - } - // Call connect methods - console.log('Reconnecting') - this._client = this._connect(this.url, this.stream, this._accessToken, this.headers, this.proxyConfig) - this._bindSocket(this._client) + setTimeout(() => { + // Skip reconnect when client is connecting. + // https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L365 + if (this._client && this._client.readyState === WS.CONNECTING) { + return + } + + if (this._reconnectCurrentAttempts < this._reconnectMaxAttempts) { + this._reconnectCurrentAttempts++ + this._clearBinding() + if (this._client) { + // In reconnect, we want to close the connection immediately, + // because recoonect is necessary when some problems occur. + this._client.terminate() } - }, this._reconnectInterval) - } + // Call connect methods + console.log('Reconnecting') + this._client = this._connect(this.url, this.stream, this._accessToken, this.headers, this.proxyConfig) + this._bindSocket(this._client) + } + }, this._reconnectInterval) } /** @@ -243,16 +247,18 @@ export default class WebSocket extends EventEmitter { // Block multiple calling, if multiple pong event occur. // It the duration is less than interval, through ping. if (now.diff(timestamp) > this._heartbeatInterval - 1000 && !this._connectionClosed) { - if (this._client) { + // Skip ping when client is connecting. + // https://github.com/websockets/ws/blob/7.2.1/lib/websocket.js#L289 + if (this._client && this._client.readyState !== WS.CONNECTING) { this._pongWaiting = true this._client.ping('') + setTimeout(() => { + if (this._pongWaiting) { + this._pongWaiting = false + this._reconnect() + } + }, 10000) } - setTimeout(() => { - if (this._pongWaiting) { - this._pongWaiting = false - this._reconnect() - } - }, 10000) } } } diff --git a/yarn.lock b/yarn.lock index 9bb5fc9..9eeab4e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -643,7 +643,7 @@ astral-regex@^1.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9" integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg== -async-limiter@^1.0.0, async-limiter@~1.0.0: +async-limiter@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8" integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg== @@ -4404,12 +4404,10 @@ ws@^5.2.0: dependencies: async-limiter "~1.0.0" -ws@^7.0.1: - version "7.2.0" - resolved "https://registry.yarnpkg.com/ws/-/ws-7.2.0.tgz#422eda8c02a4b5dba7744ba66eebbd84bcef0ec7" - integrity sha512-+SqNqFbwTm/0DC18KYzIsMTnEWpLwJsiasW/O17la4iDRRIO9uaHbvKiAS3AHgTiuuWerK/brj4O6MYZkei9xg== - dependencies: - async-limiter "^1.0.0" +ws@^7.2.1: + version "7.2.1" + resolved "https://registry.yarnpkg.com/ws/-/ws-7.2.1.tgz#03ed52423cd744084b2cf42ed197c8b65a936b8e" + integrity sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A== xml-name-validator@^3.0.0: version "3.0.0"