-
Notifications
You must be signed in to change notification settings - Fork 10
/
signalr.service.ts
38 lines (31 loc) · 893 Bytes
/
signalr.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
import { Injectable } from '@angular/core';
import * as signalR from '@aspnet/signalr';
import { Subject, Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { Message } from '../message';
@Injectable({
providedIn: 'root'
})
export class SignalRService {
private message$: Subject<Message>;
private connection: signalR.HubConnection;
constructor() {
this.message$ = new Subject<Message>();
this.connection = new signalR.HubConnectionBuilder()
.withUrl(environment.hubUrl)
.build();
this.connect();
}
private connect() {
this.connection.start().catch(err => console.log(err));
this.connection.on('SendMessage', (message) => {
this.message$.next(message);
});
}
public getMessage(): Observable<Message> {
return this.message$;
}
public disconnect() {
this.connection.stop();
}
}