-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatus.component.ts
55 lines (43 loc) · 1.26 KB
/
status.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {filter, map} from 'rxjs/operators';
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs';
import { StompService, StompState } from '@stomp/ng2-stompjs';
@Component({
selector: 'app-status',
templateUrl: './status.component.html',
styleUrls: ['./status.component.css']
})
export class StatusComponent implements OnInit {
public state: Observable<string>;
/** Constructor */
constructor(private _stompService: StompService) { }
ngOnInit() {
console.log('Status init');
this.state = this._stompService.state.pipe(
map((state: number) => {
console.log(`Current state: ${StompState[state]}`);
return StompState[state];
})
);
const MAX_RETRIES = 3;
let numRetries = MAX_RETRIES;
this._stompService.state.pipe(
filter((state: number) => state === StompState.CLOSED)
).subscribe(() => {
console.log(`Will retry ${numRetries} times`);
if (numRetries <= 0) {
this._stompService.disconnect();
}
numRetries--;
});
this._stompService.connectObservable.subscribe(() => {
numRetries = MAX_RETRIES;
});
}
connect() {
this._stompService.initAndConnect();
}
disconnect() {
this._stompService.disconnect();
}
}