Skip to content

Instantly share code, notes, and snippets.

@Danielfenghk
Forked from weihungchin/rxjs-websocket-5.js
Created September 6, 2022 07:32
Show Gist options
  • Save Danielfenghk/6705bb3ef34e8ffd6078d7860a04a1eb to your computer and use it in GitHub Desktop.
Save Danielfenghk/6705bb3ef34e8ffd6078d7860a04a1eb to your computer and use it in GitHub Desktop.
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