const routes: Routes = [
{ path: 'user', component: UserComponent },
{ path: 'chatrooms', component: ChatroomsComponent },
- { path: 'chatroom/:id', component: ChatroomComponent },
+ { path: 'chatroom/:shard/:id', component: ChatroomComponent },
{ path: '', redirectTo: '/user', pathMatch: 'full' },
];
return this.http.get<Chatroom[]>(this.backendUri + 'list');
}
- getChatroom(id: String): Observable<Chatroom> {
- return this.http.get<Chatroom>(this.backendUri + id);
+ getChatroom(shard: string, id: string): Observable<Chatroom> {
+ return this.http.get<Chatroom>(
+ this.backendUri + id,
+ { headers: { 'X-Shard': shard }});
}
- listen(id: String): Observable<Message> {
+ listen(shard: string, id: string): Observable<Message> {
let observable = new Observable<Message>(
(observer) => {
this.channel = observer;
let uri: string = this.backendUri + id + '/listen';
let service = this;
- fetchEventSource(uri, {
+ fetchEventSource(uri,{
+ headers: { 'X-Shard': shard },
async onopen(response) {
if (response.ok && response.status === 200) {
console.log('Opend channel ' + uri, response);
export interface Chatroom
{
id: string,
- name: string
+ name: string,
+ shard: number
}
})
export class ChatroomComponent implements OnInit, OnDestroy {
- chatroom: Chatroom = { id: 'FOO', name: 'BAR' };
+ chatroom: Chatroom = { id: 'FOO', name: 'BAR', shard: -1 };
messages: Message[] = [];
constructor(
ngOnDestroy() {
this.chatroomsService.unlisten();
- this.chatroom = { id: 'FOO', name: 'BAR' };
+ this.chatroom = { id: 'FOO', name: 'BAR', shard: -1 };
}
getChatroom(): void {
const id: string | null = this.route.snapshot.paramMap.get('id');
- if (id === null) {
- console.log("ID for chatroom is missing in URI");
+ const shard: string | null = this.route.snapshot.paramMap.get('shard');
+ if (id === null || shard === null) {
+ console.log("ID and shard for chatroom is missing in URI");
this.router.navigate(['chatrooms']);
}
else {
this.chatroomsService
- .getChatroom(id)
+ .getChatroom(shard, id)
.subscribe(chatroom => this.chatroom = chatroom);
this.chatroomsService
- .listen(id)
+ .listen(shard, id)
.subscribe({
next: message => this.zone.run(() => this.messages.push(message)),
error: err => console.error('something wrong occurred: ' + err) });
<h1 class="h5">Please Choose a Chat-Room</h1>
</div>
<div class="card-body d-grid gap-1">
- <button *ngFor="let chatroom of chatrooms" routerLink="/chatroom/{{chatroom.id}}" type="button" class="btn btn-outline-primary">{{chatroom.name}}</button>
+ <button *ngFor="let chatroom of chatrooms" routerLink="/chatroom/{{chatroom.shard}}/{{chatroom.id}}" type="button" class="btn btn-outline-primary">{{chatroom.name}}</button>
</div>
</div>