websockets should now reconnect automatically
This commit is contained in:
parent
28d35d037c
commit
f524ab93c6
|
@ -19,6 +19,7 @@
|
|||
"buffer-layout": "^1.2.0",
|
||||
"commander": "^6.2.0",
|
||||
"dotenv": "^8.2.0",
|
||||
"reconnecting-websocket": "^4.4.0",
|
||||
"solray": "https://github.com/blockworks-foundation/solray.git",
|
||||
"winston": "^3.3.3",
|
||||
"ws": "^7.4.1"
|
||||
|
|
22
src/feeds.ts
22
src/feeds.ts
|
@ -1,4 +1,5 @@
|
|||
import WebSocket from "ws"
|
||||
import WebSocket from 'ws'
|
||||
import ReconnectingWebSocket from 'reconnecting-websocket'
|
||||
import EventEmitter from "events"
|
||||
import { eventsIter, median } from "./utils"
|
||||
|
||||
|
@ -21,7 +22,7 @@ export interface IPriceFeed {
|
|||
export abstract class PriceFeed {
|
||||
public emitter = new EventEmitter()
|
||||
|
||||
protected conn!: WebSocket
|
||||
protected conn!: ReconnectingWebSocket
|
||||
protected connected!: Promise<void>
|
||||
|
||||
protected abstract get log(): winston.Logger
|
||||
|
@ -34,8 +35,8 @@ export abstract class PriceFeed {
|
|||
this.log.debug("connecting", { baseurl: this.baseurl })
|
||||
|
||||
this.connected = new Promise<void>((resolve) => {
|
||||
const conn = new WebSocket(this.baseurl)
|
||||
conn.on("open", () => {
|
||||
const conn = new ReconnectingWebSocket(this.baseurl, [], { WebSocket })
|
||||
conn.addEventListener("open", () => {
|
||||
this.log.debug("connected")
|
||||
|
||||
this.conn = conn
|
||||
|
@ -47,15 +48,16 @@ export abstract class PriceFeed {
|
|||
resolve()
|
||||
})
|
||||
|
||||
conn.on("close", () => {
|
||||
// TODO: auto-reconnect & re-subscribe
|
||||
conn.addEventListener("close", () => {
|
||||
console.log(`socket ${this.baseurl} closed`)
|
||||
})
|
||||
|
||||
conn.on("message", async (data) => {
|
||||
// this.log.debug("raw price update", { data })
|
||||
|
||||
const price = this.parseMessage(data)
|
||||
conn.addEventListener("error", (e) => {
|
||||
console.log(`socket ${this.baseurl} error: ${e}`)
|
||||
})
|
||||
|
||||
conn.addEventListener("message", (msg) => {
|
||||
const price = this.parseMessage(msg.data)
|
||||
if (price) {
|
||||
this.onMessage(price)
|
||||
}
|
||||
|
|
|
@ -1603,6 +1603,11 @@ readdirp@~3.5.0:
|
|||
dependencies:
|
||||
picomatch "^2.2.1"
|
||||
|
||||
reconnecting-websocket@^4.4.0:
|
||||
version "4.4.0"
|
||||
resolved "https://registry.yarnpkg.com/reconnecting-websocket/-/reconnecting-websocket-4.4.0.tgz#3b0e5b96ef119e78a03135865b8bb0af1b948783"
|
||||
integrity sha512-D2E33ceRPga0NvTDhJmphEgJ7FUYF0v4lr1ki0csq06OdlxKfugGzN0dSkxM/NfqCxYELK4KcaTOUOjTV6Dcng==
|
||||
|
||||
redent@^1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/redent/-/redent-1.0.0.tgz#cf916ab1fd5f1f16dfb20822dd6ec7f730c2afde"
|
||||
|
@ -1722,9 +1727,9 @@ simple-swizzle@^0.2.2:
|
|||
dependencies:
|
||||
is-arrayish "^0.3.1"
|
||||
|
||||
"solray@git+https://github.com/defactojob/solray":
|
||||
"solray@https://github.com/blockworks-foundation/solray.git":
|
||||
version "0.0.1"
|
||||
resolved "git+https://github.com/defactojob/solray#1884a52016b53ea3b12ff5537fd7b14b0f0d8ffc"
|
||||
resolved "https://github.com/blockworks-foundation/solray.git#60b321ca81395cc2404132634f62e06f160a0f82"
|
||||
dependencies:
|
||||
"@solana/web3.js" "^0.91.3"
|
||||
bip32 "^2.0.6"
|
||||
|
|
Loading…
Reference in New Issue