Skip to content

Commit a8ced02

Browse files
committed
Watching Connection Status
1 parent 7fa3c74 commit a8ced02

7 files changed

+93
-1
lines changed

src/app/app.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ <h1>
33
Welcome to {{ title }}!
44
</h1>
55
</div>
6+
<app-status></app-status>
67
<app-messages></app-messages>

src/app/app.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { InjectableRxStompConfig, RxStompService, rxStompServiceFactory } from '
66
import { AppComponent } from './app.component';
77
import { myRxStompConfig } from './my-rx-stomp.config';
88
import { MessagesComponent } from './messages/messages.component';
9+
import { StatusComponent } from './status/status.component';
910

1011
@NgModule({
1112
declarations: [
1213
AppComponent,
13-
MessagesComponent
14+
MessagesComponent,
15+
StatusComponent
1416
],
1517
imports: [
1618
BrowserModule

src/app/status/status.component.css

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
div#status {
2+
text-align: center;
3+
font-family: Arial, serif;
4+
font-size: 1.2em;
5+
}
6+
7+
div#indicator {
8+
width: 12px;
9+
height: 12px;
10+
border-radius: 6px;
11+
margin: 0 10px;
12+
display: inline-block;
13+
}
14+
15+
div.CLOSED#indicator {
16+
background: red;
17+
}
18+
19+
div.CONNECTING#indicator, div.CLOSING#indicator {
20+
background: yellow;
21+
}
22+
23+
div.OPEN#indicator {
24+
background: green;
25+
}
26+
27+
button {
28+
margin: 0 10px;
29+
font-size: 1em;
30+
}

src/app/status/status.component.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div id="status">
2+
<div id="indicator" class="{{connectionStatus$|async}}"></div>
3+
<span>{{connectionStatus$|async}}</span>
4+
<button class="btn" (click)="rxStompService.activate()">Activate</button>
5+
<button class="btn" (click)="rxStompService.deactivate()">DeActivate</button>
6+
<button class="btn" (click)="rxStompService.stompClient.forceDisconnect()">Simulate Error</button>
7+
</div>
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2+
3+
import { StatusComponent } from './status.component';
4+
5+
describe('StatusComponent', () => {
6+
let component: StatusComponent;
7+
let fixture: ComponentFixture<StatusComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [ StatusComponent ]
12+
})
13+
.compileComponents();
14+
}));
15+
16+
beforeEach(() => {
17+
fixture = TestBed.createComponent(StatusComponent);
18+
component = fixture.componentInstance;
19+
fixture.detectChanges();
20+
});
21+
22+
it('should create', () => {
23+
expect(component).toBeTruthy();
24+
});
25+
});

src/app/status/status.component.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import {RxStompService} from '@stomp/ng2-stompjs';
3+
import {Observable} from 'rxjs';
4+
import {map} from 'rxjs/operators';
5+
import {RxStompState} from '@stomp/rx-stomp';
6+
7+
@Component({
8+
selector: 'app-status',
9+
templateUrl: './status.component.html',
10+
styleUrls: ['./status.component.css']
11+
})
12+
export class StatusComponent implements OnInit {
13+
public connectionStatus$: Observable<string>;
14+
15+
constructor(public rxStompService: RxStompService) {
16+
this.connectionStatus$ = rxStompService.connectionState$.pipe(map((state) => {
17+
return RxStompState[state];
18+
}));
19+
}
20+
21+
ngOnInit() {
22+
}
23+
24+
}

src/styles.css

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/* You can add global styles to this file, and also import other style files */
2+
button.btn {
3+
font-size: 1.1em;
4+
}

0 commit comments

Comments
 (0)