1 import { wsUri } from '../env';
6 } from 'lemmy-js-client';
7 import { isBrowser } from '../utils';
8 import { Observable } from 'rxjs';
9 import { share } from 'rxjs/operators';
12 default as ReconnectingWebSocket,
13 } from 'reconnecting-websocket';
15 export class WebSocketService {
16 private static _instance: WebSocketService;
17 public ws: ReconnectingWebSocket;
18 public wsOptions: WSOptions = {
19 connectionTimeout: 5000,
22 public subject: Observable<any>;
24 public admins: UserViewSafe[];
25 public banned: UserViewSafe[];
26 public client = new LemmyWebsocket();
28 private constructor() {
29 this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
30 let firstConnect = true;
32 this.subject = new Observable((obs: any) => {
33 this.ws.onmessage = e => {
34 obs.next(JSON.parse(e.data));
36 this.ws.onopen = () => {
37 console.log(`Connected to ${wsUri}`);
40 let res: WebSocketJsonResponse<any> = {
51 public static get Instance() {
52 return this._instance || (this._instance = new this());
57 window.onbeforeunload = () => {
58 WebSocketService.Instance.ws.close();