diff --git a/README.md b/README.md index 468f04d..988f4d9 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # Orion Protocol SDK +[![npm version](https://img.shields.io/npm/v/@orionprotocol/sdk.svg)](https://www.npmjs.com/package/@orionprotocol/sdk) +[![Downloads](https://img.shields.io/npm/dm/@orionprotocol/sdk.svg)](https://www.npmjs.com/package/@orionprotocol/sdk) + # Install ```console diff --git a/package.json b/package.json index bcff9f8..138c7c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.2.8", + "version": "0.3.0", "description": "Orion Protocol SDK", "main": "./lib/umd/index.js", "module": "./lib/esm/index.js", diff --git a/src/services/OrionAggregator/ws/index.ts b/src/services/OrionAggregator/ws/index.ts index b13b341..eba7f4f 100644 --- a/src/services/OrionAggregator/ws/index.ts +++ b/src/services/OrionAggregator/ws/index.ts @@ -90,10 +90,12 @@ class OrionAggregatorWS { private onError?: (err: string) => void; - constructor(url: string, chainId: SupportedChainId, onError?: (err: string) => void) { + private readonly wsUrl: string; + + constructor(wsUrl: string, chainId: SupportedChainId, onError?: (err: string) => void) { + this.wsUrl = wsUrl; this.chainId = chainId; this.onError = onError; - this.init(url); } sendRaw(data: string | ArrayBufferLike | Blob | ArrayBufferView) { @@ -120,6 +122,7 @@ class OrionAggregatorWS { type: T, subscription: Subscription[T], ) { + if (!this.ws) this.init(); this.send({ T: type, ...('payload' in subscription) && { @@ -150,11 +153,16 @@ class OrionAggregatorWS { } } - init(url: string) { - this.ws = new WebSocket(url); - this.ws.onclose = () => { + destroy() { + this.ws?.close(4000); + delete this.ws; + } + + init() { + this.ws = new WebSocket(this.wsUrl); + this.ws.onclose = (e) => { console.log(`Orion Aggregator ${this.chainId} WS Connection closed`); - this.init(url); + if (e.code !== 4000) this.init(); }; this.ws.onopen = () => { console.log(`Orion Aggregator ${this.chainId} WS Connection established`);