Skip to content
Snippets Groups Projects
Commit ddf03f72 authored by Tom Käsler's avatar Tom Käsler
Browse files

WebSocket Connector

parent a8b566d4
Branches
Tags
No related merge requests found
...@@ -14,6 +14,7 @@ import { CommentService } from './services/http/comment.service'; ...@@ -14,6 +14,7 @@ import { CommentService } from './services/http/comment.service';
import { DataStoreService } from './services/util/data-store.service'; import { DataStoreService } from './services/util/data-store.service';
import { ContentService } from './services/http/content.service'; import { ContentService } from './services/http/content.service';
import { ContentAnswerService } from './services/http/content-answer.service'; import { ContentAnswerService } from './services/http/content-answer.service';
import { WsConnectorService } from './services/websockets/ws-connector.service';
import { UserActivationComponent } from './components/home/_dialogs/user-activation/user-activation.component'; import { UserActivationComponent } from './components/home/_dialogs/user-activation/user-activation.component';
import { AuthenticationInterceptor } from './interceptors/authentication.interceptor'; import { AuthenticationInterceptor } from './interceptors/authentication.interceptor';
import { EssentialsModule } from './components/essentials/essentials.module'; import { EssentialsModule } from './components/essentials/essentials.module';
...@@ -24,8 +25,6 @@ import { LanguageService } from './services/util/language.service'; ...@@ -24,8 +25,6 @@ import { LanguageService } from './services/util/language.service';
import { MarkdownService, MarkedOptions } from 'ngx-markdown'; import { MarkdownService, MarkedOptions } from 'ngx-markdown';
import { NewLandingComponent } from './components/home/new-landing/new-landing.component'; import { NewLandingComponent } from './components/home/new-landing/new-landing.component';
import { HomePageComponent } from './components/home/home-page/home-page.component'; import { HomePageComponent } from './components/home/home-page/home-page.component';
import { InjectableRxStompConfig, RxStompService, rxStompServiceFactory } from '@stomp/ng2-stompjs';
import { myRxStompConfig } from './rx-stomp.config';
import { AppConfig } from './app.config'; import { AppConfig } from './app.config';
export function dialogClose(dialogResult: any) { export function dialogClose(dialogResult: any) {
...@@ -67,15 +66,7 @@ export function initializeApp(appConfig: AppConfig) { ...@@ -67,15 +66,7 @@ export function initializeApp(appConfig: AppConfig) {
useClass: AuthenticationInterceptor, useClass: AuthenticationInterceptor,
multi: true multi: true
}, },
{ WsConnectorService,
provide: InjectableRxStompConfig,
useValue: myRxStompConfig
},
{
provide: RxStompService,
useFactory: rxStompServiceFactory,
deps: [InjectableRxStompConfig]
},
NotificationService, NotificationService,
AuthenticationService, AuthenticationService,
AuthenticationGuard, AuthenticationGuard,
...@@ -88,6 +79,7 @@ export function initializeApp(appConfig: AppConfig) { ...@@ -88,6 +79,7 @@ export function initializeApp(appConfig: AppConfig) {
MarkdownService, MarkdownService,
MarkedOptions, MarkedOptions,
UserService, UserService,
WsConnectorService,
{ {
provide: MatDialogRef, provide: MatDialogRef,
useValue: { useValue: {
......
...@@ -3,7 +3,6 @@ import { Comment } from '../../../models/comment'; ...@@ -3,7 +3,6 @@ import { Comment } from '../../../models/comment';
import { CommentService } from '../../../services/http/comment.service'; import { CommentService } from '../../../services/http/comment.service';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { LanguageService } from '../../../services/util/language.service'; import { LanguageService } from '../../../services/util/language.service';
import { RxStompService } from '@stomp/ng2-stompjs';
import { Message } from '@stomp/stompjs'; import { Message } from '@stomp/stompjs';
import { SubmitCommentComponent } from '../_dialogs/submit-comment/submit-comment.component'; import { SubmitCommentComponent } from '../_dialogs/submit-comment/submit-comment.component';
import { MatDialog } from '@angular/material'; import { MatDialog } from '@angular/material';
...@@ -30,7 +29,6 @@ export class CommentListComponent implements OnInit { ...@@ -30,7 +29,6 @@ export class CommentListComponent implements OnInit {
private translateService: TranslateService, private translateService: TranslateService,
public dialog: MatDialog, public dialog: MatDialog,
protected langService: LanguageService, protected langService: LanguageService,
private rxStompService: RxStompService,
private wsCommentService: WsCommentServiceService, private wsCommentService: WsCommentServiceService,
private authenticationService: AuthenticationService) { private authenticationService: AuthenticationService) {
langService.langEmitter.subscribe(lang => translateService.use(lang)); langService.langEmitter.subscribe(lang => translateService.use(lang));
...@@ -40,7 +38,7 @@ export class CommentListComponent implements OnInit { ...@@ -40,7 +38,7 @@ export class CommentListComponent implements OnInit {
this.roomId = localStorage.getItem(`roomId`); this.roomId = localStorage.getItem(`roomId`);
this.comments = []; this.comments = [];
this.hideCommentsList = false; this.hideCommentsList = false;
this.rxStompService.watch(`/topic/${this.roomId}.comment.stream`).subscribe((message: Message) => { this.wsCommentService.getCommentStream(this.roomId).subscribe((message: Message) => {
this.parseIncomingMessage(message); this.parseIncomingMessage(message);
}); });
this.getComments(); this.getComments();
......
import { InjectableRxStompConfig } from '@stomp/ng2-stompjs'; import { RxStompConfig } from '@stomp/rx-stomp';
export const myRxStompConfig: InjectableRxStompConfig = { export const ARSRxStompConfig: RxStompConfig = {
// Which server? // Which server?
brokerURL: `ws://${window.location.hostname}:8080/ws/websocket`, brokerURL: `ws://${window.location.hostname}:8080/ws/websocket`,
connectHeaders: {
login: 'guest',
password: 'guest'
},
// How often to heartbeat? // How often to heartbeat?
// Interval in milliseconds, set to 0 to disable // Interval in milliseconds, set to 0 to disable
heartbeatIncoming: 0, // Typical value 0 - disabled heartbeatIncoming: 0, // Typical value 0 - disabled
......
...@@ -130,4 +130,8 @@ export class AuthenticationService { ...@@ -130,4 +130,8 @@ export class AuthenticationService {
get watchUser() { get watchUser() {
return this.user.asObservable(); return this.user.asObservable();
} }
getUserAsSubject(): BehaviorSubject<User> {
return this.user;
}
} }
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Comment } from '../../models/comment'; import { Comment } from '../../models/comment';
import { RxStompService } from '@stomp/ng2-stompjs'; import { WsConnectorService } from '../../services/websockets/ws-connector.service';
import { CreateComment } from '../../models/messages/create-comment'; import { CreateComment } from '../../models/messages/create-comment';
import { PatchComment } from '../../models/messages/patch-comment'; import { PatchComment } from '../../models/messages/patch-comment';
import { TSMap } from 'typescript-map'; import { TSMap } from 'typescript-map';
import { UpVote } from '../../models/messages/up-vote'; import { UpVote } from '../../models/messages/up-vote';
import { DownVote } from '../../models/messages/down-vote'; import { DownVote } from '../../models/messages/down-vote';
import { Observable } from 'rxjs';
import { IMessage } from '@stomp/stompjs';
@Injectable({ @Injectable({
...@@ -13,17 +15,11 @@ import { DownVote } from '../../models/messages/down-vote'; ...@@ -13,17 +15,11 @@ import { DownVote } from '../../models/messages/down-vote';
}) })
export class WsCommentServiceService { export class WsCommentServiceService {
constructor(private rxStompService: RxStompService) { } constructor(private wsConnector: WsConnectorService) { }
add(comment: Comment): void { add(comment: Comment): void {
const message = new CreateComment(comment.roomId, comment.userId, comment.body); const message = new CreateComment(comment.roomId, comment.userId, comment.body);
this.rxStompService.publish({ this.wsConnector.send(`/queue/comment.command.create`, JSON.stringify(message));
destination: `/queue/comment.command.create`,
body: JSON.stringify(message),
headers: {
'content-type': 'application/json'
}
});
} }
toggleRead(comment: Comment): Comment { toggleRead(comment: Comment): Comment {
...@@ -53,34 +49,20 @@ export class WsCommentServiceService { ...@@ -53,34 +49,20 @@ export class WsCommentServiceService {
voteUp(comment: Comment): void { voteUp(comment: Comment): void {
const message = new UpVote(comment.userId, comment.id); const message = new UpVote(comment.userId, comment.id);
this.rxStompService.publish({ this.wsConnector.send(`/queue/vote.command.upvote`, JSON.stringify(message));
destination: `/queue/vote.command.upvote`,
body: JSON.stringify(message),
headers: {
'content-type': 'application/json'
}
});
} }
voteDown(comment: Comment): void { voteDown(comment: Comment): void {
const message = new DownVote(comment.userId, comment.id); const message = new DownVote(comment.userId, comment.id);
this.rxStompService.publish({ this.wsConnector.send(`/queue/vote.command.downvote`, JSON.stringify(message));
destination: `/queue/vote.command.downvote`,
body: JSON.stringify(message),
headers: {
'content-type': 'application/json'
}
});
} }
private patchComment(comment: Comment, changes: TSMap<string, any>): void { private patchComment(comment: Comment, changes: TSMap<string, any>): void {
const message = new PatchComment(comment.id, changes); const message = new PatchComment(comment.id, changes);
this.rxStompService.publish({ this.wsConnector.send(`/queue/comment.command.patch`, JSON.stringify(message));
destination: `/queue/comment.command.patch`, }
body: JSON.stringify(message),
headers: { getCommentStream(roomId: string): Observable<IMessage> {
'content-type': 'application/json' return this.wsConnector.getWatcher(`/topic/${roomId}.comment.stream`);
}
});
} }
} }
import { Injectable } from '@angular/core';
import { RxStomp } from '@stomp/rx-stomp';
import { AuthenticationService } from '../http/authentication.service';
import { User } from '../../models/user';
import { ARSRxStompConfig } from '../../rx-stomp.config';
import { Observable } from 'rxjs';
import { IMessage, StompHeaders } from '@stomp/stompjs';
@Injectable({
providedIn: 'root'
})
export class WsConnectorService {
private client: RxStomp;
private headers = {
'content-type': 'application/json',
'ars-user-id': ''
};
constructor(
private authService: AuthenticationService
) {
this.client = new RxStomp();
const userSubject = authService.getUserAsSubject();
userSubject.subscribe((user: User) => {
if (this.client.connected) {
this.client.deactivate();
}
if (user && user.id) {
const copiedConf = ARSRxStompConfig;
copiedConf.connectHeaders.token = user.token;
this.headers = {
'content-type': 'application/json',
'ars-user-id': '' + user.id
};
this.client.configure(copiedConf);
this.client.activate();
}
});
}
public send(destination: string, body: string): void {
if (this.client.connected) {
this.client.publish({
destination: destination,
body: body,
headers: this.headers
});
}
}
public getWatcher(topic: string): Observable<IMessage> {
if (this.client.connected) {
return this.client.watch(topic, this.headers);
}
}
}
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { RxStompService } from '@stomp/ng2-stompjs'; import { WsConnectorService } from '../../services/websockets/ws-connector.service';
import { CreateFeedback } from '../../models/messages/create-feedback'; import { CreateFeedback } from '../../models/messages/create-feedback';
import { GetFeedback } from '../../models/messages/get-feedback'; import { GetFeedback } from '../../models/messages/get-feedback';
...@@ -7,22 +7,16 @@ import { GetFeedback } from '../../models/messages/get-feedback'; ...@@ -7,22 +7,16 @@ import { GetFeedback } from '../../models/messages/get-feedback';
providedIn: 'root' providedIn: 'root'
}) })
export class WsFeedbackService { export class WsFeedbackService {
constructor(private rxStompService: RxStompService) {} constructor(private wsConnector: WsConnectorService) {}
send(feedback: number, roomId: string) { send(feedback: number, roomId: string) {
const createFeedback = new CreateFeedback(feedback); const createFeedback = new CreateFeedback(feedback);
this.rxStompService.publish({ this.wsConnector.send(`/backend/queue/${roomId}.feedback.command`, JSON.stringify(createFeedback));
destination: `/backend/queue/${roomId}.feedback.command`,
body: JSON.stringify(createFeedback)
});
} }
get(roomId: string) { get(roomId: string) {
const getFeedback = new GetFeedback(); const getFeedback = new GetFeedback();
this.rxStompService.publish({ this.wsConnector.send(`/backend/queue/${roomId}.feedback.query`, JSON.stringify(getFeedback));
destination: `/backend/queue/${roomId}.feedback.query`,
body: JSON.stringify(getFeedback)
});
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment