|
|
|
@ -1,20 +1,76 @@
|
|
|
|
|
import { webSocket } from 'rxjs/webSocket';
|
|
|
|
|
import { of, timer, concatMap } from 'rxjs';
|
|
|
|
|
import { filter, buffer, map, tap, retryWhen, retry, delay, take, } from 'rxjs/operators';
|
|
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
|
import { of, timer, concatMap, EMPTY, takeWhile, concat } from 'rxjs';
|
|
|
|
|
import { filter, buffer, map, tap, retryWhen, retry, delay, take, catchError } from 'rxjs/operators';
|
|
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
|
|
|
|
|
export class RealTimeAPI {
|
|
|
|
|
constructor(param) {
|
|
|
|
|
this.webSocket = webSocket(param);
|
|
|
|
|
constructor(param, onOpenCallback, onCloseCallback, onRetryCallback) {
|
|
|
|
|
this.onOpenCallback = onOpenCallback;
|
|
|
|
|
this.onCloseCallback = onCloseCallback;
|
|
|
|
|
this.onErrorCallback = onRetryCallback;
|
|
|
|
|
this.webSocket = webSocket({
|
|
|
|
|
...param,
|
|
|
|
|
openObserver: {
|
|
|
|
|
next: () => {
|
|
|
|
|
this.onOpen();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
closeObserver: {
|
|
|
|
|
next: () => {
|
|
|
|
|
this.onClose();
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
this.retryCount = 0;
|
|
|
|
|
}
|
|
|
|
|
onOpen() {
|
|
|
|
|
console.log(
|
|
|
|
|
`%c WebSocket connection opened `,
|
|
|
|
|
'background:#41b883 ; padding: 1px; border-radius: 3px; color: #fff',
|
|
|
|
|
);
|
|
|
|
|
if (this.onOpenCallback) {
|
|
|
|
|
this.onOpenCallback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onClose() {
|
|
|
|
|
console.log(
|
|
|
|
|
`%c WebSocket connection closed `,
|
|
|
|
|
'background:#35495e ; padding: 1px; border-radius: 3px; color: #fff'
|
|
|
|
|
);
|
|
|
|
|
if (this.onCloseCallback) {
|
|
|
|
|
this.onCloseCallback(this.retryCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onRetry(i) {
|
|
|
|
|
this.retryCount = i;
|
|
|
|
|
if (this.onErrorCallback) {
|
|
|
|
|
this.onErrorCallback(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getObservable() {
|
|
|
|
|
return this.webSocket.pipe(
|
|
|
|
|
retry(10)
|
|
|
|
|
// retry({
|
|
|
|
|
// count: 10,
|
|
|
|
|
// delay: 3000, // () => timer(3000)
|
|
|
|
|
// })
|
|
|
|
|
// retry(10)
|
|
|
|
|
retry({
|
|
|
|
|
count: 10,
|
|
|
|
|
// delay: 3000,
|
|
|
|
|
delay: (errors, index) => {
|
|
|
|
|
this.onRetry(index);
|
|
|
|
|
return timer(3000);
|
|
|
|
|
},
|
|
|
|
|
resetOnSuccess: true,
|
|
|
|
|
}),
|
|
|
|
|
catchError((error) => {
|
|
|
|
|
this.retryCount = 0;
|
|
|
|
|
this.onRetry(-1);
|
|
|
|
|
console.log(
|
|
|
|
|
`%c All retries exhausted `,
|
|
|
|
|
'background:#fb923c ; padding: 1px; border-radius: 3px; color: #fff'
|
|
|
|
|
);
|
|
|
|
|
return EMPTY;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -72,7 +128,6 @@ export class RealTimeAPI {
|
|
|
|
|
return this.getObservableFilteredByMessageType('ping').pipe(tap(() => this.sendMessage({ msg: 'pong' })));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
callMethod(method, ...params) {
|
|
|
|
|
let id = 'uuid()';
|
|
|
|
|
this.sendMessage({
|
|
|
|
|