Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component({
- selector: 'app-webchat',
- templateUrl: './webchat.component.html',
- styleUrls: ['./webchat.component.scss']
- })
- export class WebchatComponent implements OnInit, OnDestroy {
- messages: ChatMessage[] = [];
- author: string;
- messageValue: string;
- subscription: Subscription;
- authorNicknameDisplayed = true;
- constructor(private rxStompService: RxStompService) {
- this.subscription = this.rxStompService.watch('/topic/public').subscribe((receivedMessage: Message) => {
- const message: ChatMessage = JSON.parse(receivedMessage.body);
- this.messages.push(message);
- });
- }
- ngOnInit() {
- }
- sendMessage() {
- const message: ChatMessage = { message: this.messageValue, author: this.author, type: ChatMessageType.MESSAGE };
- this.rxStompService.publish({destination: '/app/chat.sendMessage', body: JSON.stringify(message)});
- this.messageValue = '';
- }
- onAuthorSet() {
- const joinMessage = { message: '', author: this.author, type: ChatMessageType.JOIN };
- this.rxStompService.publish({destination: '/app/chat.addUser', body: JSON.stringify(joinMessage)});
- this.authorNicknameDisplayed = false;
- }
- ngOnDestroy() {
- this.subscription.unsubscribe();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement