-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-hub-service.ts
40 lines (29 loc) · 1.01 KB
/
chat-hub-service.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
import { singleton, autoinject } from "aurelia-framework";
import { EventAggregator } from "aurelia-event-aggregator";
import { HubConnectionBuilder, LogLevel, HubConnection } from '@aspnet/signalr';
import { AuthService } from "../auth-service";
@singleton()
@autoinject()
export class ChatHubService {
private connection: HubConnection;
constructor(private authService: AuthService, private ea: EventAggregator) {
this.connection = new HubConnectionBuilder()
.withUrl('/chat')
.configureLogging(LogLevel.Information)
.build();
this.connection.on("updateMessages", (data) => this.notifier(data));
}
public start() {
return this.connection.start().catch(err => console.error(err.toString()));
}
public stop() {
return this.connection.stop();
}
public sendMessage(message: string) {
return this.connection.invoke("Send", this.authService.username, message);
}
private notifier(data: any) {
console.log(data);
this.ea.publish("Message-Received", data);
}
}