2019-03-23 01:42:57 +00:00
|
|
|
import { wsUri } from '../env';
|
2019-03-28 19:32:08 +00:00
|
|
|
import { LoginForm, RegisterForm, UserOperation, CommunityForm, PostForm, CommentForm, CommentLikeForm } from '../interfaces';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { webSocket } from 'rxjs/webSocket';
|
|
|
|
import { Subject } from 'rxjs';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-03-23 01:42:57 +00:00
|
|
|
import { UserService } from './';
|
|
|
|
|
|
|
|
export class WebSocketService {
|
|
|
|
private static _instance: WebSocketService;
|
2019-03-26 18:00:18 +00:00
|
|
|
public subject: Subject<any>;
|
2019-03-23 01:42:57 +00:00
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
this.subject = webSocket(wsUri);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
// Even tho this isn't used, its necessary to not keep reconnecting
|
|
|
|
this.subject
|
2019-03-29 04:56:23 +00:00
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(60000), take(999))))
|
2019-03-26 18:00:18 +00:00
|
|
|
.subscribe();
|
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
console.log(`Connected to ${wsUri}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get Instance(){
|
|
|
|
return this._instance || (this._instance = new this());
|
|
|
|
}
|
|
|
|
|
|
|
|
public login(loginForm: LoginForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Login, loginForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public register(registerForm: RegisterForm) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.Register, registerForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public createCommunity(communityForm: CommunityForm) {
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setAuth(communityForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommunity, communityForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public listCommunities() {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.ListCommunities, undefined));
|
|
|
|
}
|
|
|
|
|
|
|
|
public createPost(postForm: PostForm) {
|
|
|
|
this.setAuth(postForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreatePost, postForm));
|
|
|
|
}
|
|
|
|
|
|
|
|
public getPost(postId: number) {
|
2019-03-28 19:32:08 +00:00
|
|
|
let data = {id: postId, auth: UserService.Instance.auth };
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetPost, data));
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
public getCommunity(communityId: number) {
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.GetCommunity, {id: communityId}));
|
|
|
|
}
|
|
|
|
|
|
|
|
public createComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
public editComment(commentForm: CommentForm) {
|
|
|
|
this.setAuth(commentForm);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.EditComment, commentForm));
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
public likeComment(form: CommentLikeForm) {
|
|
|
|
this.setAuth(form);
|
|
|
|
this.subject.next(this.wsSendWrapper(UserOperation.CreateCommentLike, form));
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
private wsSendWrapper(op: UserOperation, data: any) {
|
|
|
|
let send = { op: UserOperation[op], data: data };
|
2019-03-23 01:42:57 +00:00
|
|
|
console.log(send);
|
|
|
|
return send;
|
|
|
|
}
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
private setAuth(obj: any) {
|
|
|
|
obj.auth = UserService.Instance.auth;
|
|
|
|
if (obj.auth == null) {
|
|
|
|
alert("Not logged in.");
|
|
|
|
throw "Not logged in";
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-03-23 01:42:57 +00:00
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
window.onbeforeunload = (e => {
|
|
|
|
WebSocketService.Instance.subject.unsubscribe();
|
|
|
|
});
|
|
|
|
|