1 import { wsUri } from '../env';
2 import { UserViewSafe, WebSocketJsonResponse } from 'lemmy-js-client';
3 import { isBrowser } from '../utils';
4 import { Observable } from 'rxjs';
5 import { share } from 'rxjs/operators';
8 default as ReconnectingWebSocket,
9 } from 'reconnecting-websocket';
11 export class WebSocketService {
12 private static _instance: WebSocketService;
13 private ws: ReconnectingWebSocket;
14 public wsOptions: WSOptions = {
15 connectionTimeout: 5000,
18 public subject: Observable<any>;
20 public admins: UserViewSafe[];
21 public banned: UserViewSafe[];
23 private constructor() {
24 this.ws = new ReconnectingWebSocket(wsUri, [], this.wsOptions);
25 let firstConnect = true;
27 this.subject = new Observable((obs: any) => {
28 this.ws.onmessage = e => {
29 obs.next(JSON.parse(e.data));
31 this.ws.onopen = () => {
32 console.log(`Connected to ${wsUri}`);
35 let res: WebSocketJsonResponse<any> = {
46 window.onbeforeunload = () => {
52 public send(data: string) {
56 public static get Instance() {
57 return this._instance || (this._instance = new this());