Last active
September 6, 2022 07:32
-
-
Save weihungchin/66c2e503776dc316772f31f315078892 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { tap, retryWhen } from 'rxjs/operators'; | |
import { webSocket } from 'rxjs/webSocket'; | |
import { timer } from 'rxjs'; | |
const wsSubjectConfig = { | |
url: 'wss://www.gasnow.org/ws/gasprice', | |
// added this to the config object | |
closeObserver: { | |
// this is triggered when connection is closed | |
next(event) { | |
if (!event.wasClean) { | |
connect(); // | |
} | |
}, | |
}, | |
} | |
const wsSubject$ = webSocket(wsSubjectConfig); | |
function connect() { | |
wsSubject$ | |
.pipe( | |
tap((data) => console.log(data)), | |
retryWhen((errors) => errors.pipe(delayWhen((val) => timer(val * 1000)))) | |
) | |
.subscribe(); | |
} | |
connect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment