Switching to websocket-ts. #247 (#515)

This commit is contained in:
Dessalines 2021-12-01 15:54:46 -05:00 committed by GitHub
parent a11cbb29c7
commit f410648e79
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 54 deletions

View file

@ -1,49 +1,53 @@
import { PersonViewSafe, WebSocketJsonResponse } from "lemmy-js-client";
import {
default as ReconnectingWebSocket,
Options as WSOptions,
} from "reconnecting-websocket";
import { Observable } from "rxjs";
import { share } from "rxjs/operators";
import {
ExponentialBackoff,
Websocket as WS,
WebsocketBuilder,
} from "websocket-ts";
import { wsUri } from "../env";
import { isBrowser } from "../utils";
export class WebSocketService {
private static _instance: WebSocketService;
private ws: ReconnectingWebSocket;
public wsOptions: WSOptions = {
connectionTimeout: 5000,
maxRetries: 10,
};
private ws: WS;
public subject: Observable<any>;
public admins: PersonViewSafe[];
public banned: PersonViewSafe[];
private constructor() {
this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
let firstConnect = true;
this.subject = new Observable((obs: any) => {
this.ws.onmessage = e => {
try {
obs.next(JSON.parse(e.data));
} catch (err) {
console.log(err);
}
};
this.ws.onopen = () => {
console.log(`Connected to ${wsUri}`);
this.ws = new WebsocketBuilder(wsUri)
.onMessage((_i, e) => {
try {
obs.next(JSON.parse(e.data.toString()));
} catch (err) {
console.log(err);
}
})
.onOpen(() => {
console.log(`Connected to ${wsUri}`);
if (!firstConnect) {
let res: WebSocketJsonResponse<any> = {
reconnect: true,
};
obs.next(res);
}
firstConnect = false;
};
if (!firstConnect) {
let res: WebSocketJsonResponse<any> = {
reconnect: true,
};
obs.next(res);
}
firstConnect = false;
})
.onRetry(() => {
console.log("Retrying websocket connection...");
})
.onClose(() => {
console.error("Websocket closed.");
})
.withBackoff(new ExponentialBackoff(100, 7))
.build();
}).pipe(share());
if (isBrowser()) {
@ -60,10 +64,6 @@ export class WebSocketService {
this.ws.send(data);
}
public closeEventListener(listener: (event: CloseEvent) => void) {
this.ws.addEventListener("close", listener);
}
public static get Instance() {
return this._instance || (this._instance = new this());
}