Skip to content

Commit 678a136

Browse files
committed
OCP-DEMO Update Board using WebSocket
1 parent eff12cd commit 678a136

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

openshift/message-board/message-board-web/src/app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { AppRoutingModule } from './app-routing.module';
1010
import { MessageService } from './message.service';
1111
import { AccountService } from './account.service';
1212
import { EventService } from './event.service';
13+
import { WebSocketService } from './websocket.service';
1314

1415
import { AppComponent } from './app.component';
1516
import { CreateAccountComponent } from './create-account/create-account.component';
@@ -38,7 +39,7 @@ import { TimeComponent } from './time/time.component';
3839
TagComponent,
3940
TimeComponent
4041
],
41-
providers: [MessageService, AccountService, EventService],
42+
providers: [MessageService, AccountService, EventService, WebSocketService],
4243
bootstrap: [AppComponent]
4344
})
4445
export class AppModule {}

openshift/message-board/message-board-web/src/app/board-detail/board-detail.component.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {Component, OnInit, Input} from '@angular/core';
22

33
import {Message} from '../message';
44
import {MessageService} from '../message.service';
5+
import {WebSocketService} from '../websocket.service';
6+
import {EventService} from '../event.service';
57

68
@Component({
79
selector: 'app-board-detail',
@@ -12,11 +14,16 @@ export class BoardDetailComponent implements OnInit {
1214
private _userName: string;
1315
messages: Message[];
1416

15-
constructor(private service: MessageService) {}
17+
constructor(private service: MessageService, private socket: WebSocketService, private eventService: EventService) {}
1618

1719
ngOnInit() {
1820
console.log('init BoardDetailComponent');
1921
this.getMessages();
22+
this.socket.ws().onmessage = (response) => {
23+
console.log('inc ', response);
24+
this.eventService.add("Board Update:" + response.data);
25+
this.messages = JSON.parse(response.data);
26+
}
2027
}
2128

2229
get userName(): string {
@@ -28,6 +35,7 @@ export class BoardDetailComponent implements OnInit {
2835
console.log('user changed: ' + this._userName + ' -> ' + userName);
2936
this._userName = userName;
3037
this.getMessages();
38+
this.socket.listenTo(userName);
3139
}
3240

3341
getMessages(): void {

openshift/message-board/message-board-web/src/app/post-message/post-message.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export class PostMessageComponent implements OnInit {
1919

2020
postMessage(): void {
2121
console.log('Posting message:' + this.text);
22-
this.service.postMessage(this.text)
23-
.subscribe(result => {
24-
this.messages.getMessages();
25-
});
22+
this.service.postMessage(this.text).subscribe();
2623
this.text = '';
2724
}
2825

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable()
4+
export class WebSocketService {
5+
6+
private socket;
7+
8+
constructor() {
9+
this.socket = new WebSocket(window.location.origin.replace("http","ws").replace("web","message") + "/message-service/board");
10+
console.log("WS created: " + this.socket);
11+
}
12+
13+
listenTo(user : String) {
14+
this.socket.onopen = () => this.socket.send(user);
15+
if (this.socket.readyState == this.socket.OPEN) {
16+
this.socket.send(user);
17+
}
18+
}
19+
20+
ws(): WebSocket {
21+
return this.socket;
22+
}
23+
24+
}

0 commit comments

Comments
 (0)