diff --git a/package.json b/package.json index 99347fa..d377d7b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.12.7", + "version": "0.12.8", "description": "Orion Protocol SDK", "main": "./lib/esm/index.js", "module": "./lib/esm/index.js", diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index 27611e2..faff4f4 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -144,6 +144,12 @@ const isSubType = (subType: string): subType is keyof Subscription => Object.val class OrionAggregatorWS { private ws: WebSocket | undefined; + // is used to make sure we do not need to renew ws subscription + // we can not be sure that onclose event will recieve our code when we do `ws.close(4000)` + // since sometimes it can be replaced with system one. + // https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006 + private isClosedIntentionally = false; + private subscriptions: Partial<{ [K in keyof Subscription]: Partial> }> = {}; @@ -254,14 +260,16 @@ class OrionAggregatorWS { } destroy() { - this.ws?.close(4000); + this.isClosedIntentionally = true; + this.ws?.close(); delete this.ws; } init(isReconnect = false) { + this.isClosedIntentionally = false; this.ws = new WebSocket(this.wsUrl); - this.ws.onclose = (e) => { - if (e.code !== 4000) this.init(true); + this.ws.onclose = () => { + if (!this.isClosedIntentionally) this.init(true); }; this.ws.onopen = () => { // Re-subscribe to all subscriptions diff --git a/src/services/PriceFeed/ws/PriceFeedSubscription.ts b/src/services/PriceFeed/ws/PriceFeedSubscription.ts index 4530915..11574a5 100644 --- a/src/services/PriceFeed/ws/PriceFeedSubscription.ts +++ b/src/services/PriceFeed/ws/PriceFeedSubscription.ts @@ -74,6 +74,12 @@ export default class PriceFeedSubscription { + this.ws.onclose = () => { if (this.heartbeatInterval) clearInterval(this.heartbeatInterval); - if (e.code !== 4000) this.init(); + if (!this.isClosedIntentionally) this.init(); }; this.heartbeatInterval = setInterval(() => { @@ -117,6 +125,7 @@ export default class PriceFeedSubscription