-
Notifications
You must be signed in to change notification settings - Fork 0
/
rig-socket.ts
50 lines (43 loc) · 1.22 KB
/
rig-socket.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
43
44
45
46
47
48
49
50
import {
HubConnection,
HubConnectionBuilder
} from '@microsoft/signalr';
import { BehaviorSubject } from 'rxjs';
import { RigOutput } from './rig-output';
import { environment } from '../brainstorm/environments/environment';
interface SocketState {
connected: boolean,
error: any | null
}
export class RigSocket {
private endpoint = `${environment.rig}rig-socket`;
private connection: HubConnection;
private output = new Array<RigOutput>();
private socket = new BehaviorSubject<SocketState>({
connected: false,
error: null
});
socket$ = this.socket.asObservable();
constructor() {
this.connection = new HubConnectionBuilder()
.withUrl(this.endpoint)
.build();
this.connection.on(
'output',
(data: RigOutput) => {
console.table(data);
this.output.push(data);
}
);
this.connection
.start()
.then(() => this.socket.next({
connected: true,
error: null
}))
.catch((error: any) => this.socket.next({
connected: false,
error
}));
}
}