PrivateMessageForm,
EditPrivateMessageForm,
GetPrivateMessagesForm,
+ UserJoinForm,
MessageType,
} from '../interfaces';
- import { webSocket } from 'rxjs/webSocket';
- import { Subject } from 'rxjs';
- import { retryWhen, delay } from 'rxjs/operators';
import { UserService } from './';
import { i18n } from '../i18next';
import { toast } from '../utils';
public banned: Array<UserView>;
private constructor() {
- this.subject = webSocket(wsUri);
-
- // Necessary to not keep reconnecting
- this.subject
- .pipe(
- retryWhen(errors =>
- errors.pipe(
- delay(1000)
- // take(999)
- )
- )
- )
- .subscribe();
-
- console.log(`Connected to ${wsUri}`);
+ this.ws = new ReconnectingWebSocket(wsUri);
+ this.ws.onopen = () => {
+ console.log(`Connected to ${wsUri}`);
+ };
+
+ this.subject = Observable.create((obs: any) => {
+ this.ws.onmessage = e => {
+ obs.next(JSON.parse(e.data));
+ };
+ }).pipe(share());
+
+ if (UserService.Instance.user) {
+ this.userJoin();
+ }
}
public static get Instance() {
return this._instance || (this._instance = new this());
}
- this.subject.next(this.wsSendWrapper(UserOperation.UserJoin, form));
+ public userJoin() {
+ let form: UserJoinForm = { auth: UserService.Instance.auth };
++ this.ws.send(this.wsSendWrapper(UserOperation.UserJoin, form));
+ }
+
public login(loginForm: LoginForm) {
- this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
+ this.ws.send(this.wsSendWrapper(UserOperation.Login, loginForm));
}
public register(registerForm: RegisterForm) {