1 import { PersonViewSafe, WebSocketJsonResponse } from "lemmy-js-client";
2 import { Observable } from "rxjs";
3 import { share } from "rxjs/operators";
9 import { wsUri } from "../env";
10 import { isBrowser } from "../utils";
12 export class WebSocketService {
13 private static _instance: WebSocketService;
15 public subject: Observable<any>;
17 public admins: PersonViewSafe[];
18 public banned: PersonViewSafe[];
20 private constructor() {
21 let firstConnect = true;
23 this.subject = new Observable((obs: any) => {
24 this.ws = new WebsocketBuilder(wsUri)
25 .onMessage((_i, e) => {
27 obs.next(JSON.parse(e.data.toString()));
33 console.log(`Connected to ${wsUri}`);
36 let res: WebSocketJsonResponse<any> = {
44 console.log("Retrying websocket connection...");
47 console.error("Websocket closed.");
49 .withBackoff(new ExponentialBackoff(100, 7))
54 window.onunload = () => {
57 // Clears out scroll positions.
58 sessionStorage.clear();
63 public send(data: string) {
67 public static get Instance() {
68 return this._instance || (this._instance = new this());