Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Component({
  2.   selector: 'app-webchat',
  3.   templateUrl: './webchat.component.html',
  4.   styleUrls: ['./webchat.component.scss']
  5. })
  6. export class WebchatComponent implements OnInit, OnDestroy {
  7.  
  8.   messages: ChatMessage[] = [];
  9.   author: string;
  10.   messageValue: string;
  11.   subscription: Subscription;
  12.   authorNicknameDisplayed = true;
  13.  
  14.   constructor(private rxStompService: RxStompService) {
  15.     this.subscription = this.rxStompService.watch('/topic/public').subscribe((receivedMessage: Message) => {
  16.       const message: ChatMessage = JSON.parse(receivedMessage.body);
  17.       this.messages.push(message);
  18.     });
  19.   }
  20.  
  21.   ngOnInit() {
  22.   }
  23.  
  24.   sendMessage() {
  25.     const message: ChatMessage = { message: this.messageValue, author: this.author, type: ChatMessageType.MESSAGE };
  26.     this.rxStompService.publish({destination: '/app/chat.sendMessage', body: JSON.stringify(message)});
  27.     this.messageValue = '';
  28.   }
  29.  
  30.   onAuthorSet() {
  31.     const joinMessage = { message: '', author: this.author, type: ChatMessageType.JOIN };
  32.     this.rxStompService.publish({destination: '/app/chat.addUser', body: JSON.stringify(joinMessage)});
  33.     this.authorNicknameDisplayed = false;
  34.   }
  35.  
  36.   ngOnDestroy() {
  37.     this.subscription.unsubscribe();
  38.   }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement