/
home.component.ts
42 lines (33 loc) · 1.14 KB
/
home.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
import { Component, OnInit } from '@angular/core';
import { HubConnection } from '@microsoft/signalr';
import * as signalR from '@microsoft/signalr';
@Component({
selector: 'app-home-component',
templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {
private _hubConnection: HubConnection | undefined;
public async: any;
message = '';
messages: string[] = [];
constructor() {
}
public sendMessage(): void {
const data = `Sent: ${this.message}`;
if (this._hubConnection) {
this._hubConnection.invoke('Send', data);
}
this.messages.push(data);
}
ngOnInit() {
this._hubConnection = new signalR.HubConnectionBuilder()
.withUrl('https://localhost:44324/loopy')
.configureLogging(signalR.LogLevel.Trace)
.build();
this._hubConnection.start().catch(err => console.error(err.toString()));
this._hubConnection.on('Send', (data: any) => {
const received = `Received: ${data}`;
this.messages.push(received);
});
}
}