File tree 1 file changed +9
-3
lines changed
1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1
- import { Component , OnInit } from '@angular/core' ;
1
+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
2
2
import { RxStompService } from '@stomp/ng2-stompjs' ;
3
3
import { Message } from '@stomp/stompjs' ;
4
+ import { Subscription } from 'rxjs' ;
4
5
5
6
@Component ( {
6
7
selector : 'app-messages' ,
7
8
templateUrl : './messages.component.html' ,
8
9
styleUrls : [ './messages.component.css' ]
9
10
} )
10
- export class MessagesComponent implements OnInit {
11
+ export class MessagesComponent implements OnInit , OnDestroy {
11
12
public receivedMessages : string [ ] = [ ] ;
13
+ private topicSubscription : Subscription ;
12
14
13
15
constructor ( private rxStompService : RxStompService ) { }
14
16
15
17
ngOnInit ( ) {
16
- this . rxStompService . watch ( '/topic/demo' ) . subscribe ( ( message : Message ) => {
18
+ this . topicSubscription = this . rxStompService . watch ( '/topic/demo' ) . subscribe ( ( message : Message ) => {
17
19
this . receivedMessages . push ( message . body ) ;
18
20
} ) ;
19
21
}
20
22
23
+ ngOnDestroy ( ) {
24
+ this . topicSubscription . unsubscribe ( ) ;
25
+ }
26
+
21
27
onSendMessage ( ) {
22
28
const message = `Message generated at ${ new Date } ` ;
23
29
this . rxStompService . publish ( { destination : '/topic/demo' , body : message } ) ;
You can’t perform that action at this time.
0 commit comments