1
1
import { EventBus } from '@/event-bus' ;
2
+ import { SIGNALR_CONFIG } from '../config' ;
2
3
import { HubConnection , HubConnectionBuilder , HubConnectionState } from '@microsoft/signalr' ;
3
4
4
5
/**
5
- * SignalR hub defaults
6
- */
7
- const _signalrConfig = {
8
- CONNECTION_DELAY : 0 ,
9
- HUB_MESSAGE_DELAY : 3000 ,
10
- BASE_URL : '/hubs/users' ,
11
- HUB_MESSAGE_TITLE : 'SignalR' ,
12
- LOGIN_USER_EVENT : 'UserLogin' ,
13
- LOGOUT_USER_EVENT : 'UserLogout' ,
14
- CLOSE_EVENT : 'CloseAllConnections'
15
- } ;
16
-
17
- /**
18
- * SignalR API abstraction layer communication - configures/manages hub connections
6
+ * SignalR API abstraction layer communication.
7
+ * Configures/manages hub connections (typescript singleton pattern).
19
8
*/
20
9
class SignalRService {
21
10
private _hubConnection : HubConnection ;
@@ -32,46 +21,43 @@ class SignalRService {
32
21
33
22
public startConnection ( ) : void {
34
23
if ( this . _hubConnection . state === HubConnectionState . Disconnected ) {
35
- setTimeout ( ( ) => {
36
- this . _hubConnection . start ( ) . catch ( ( e ) => console . error ( e ) ) ;
37
- } , _signalrConfig . CONNECTION_DELAY ) ;
24
+ this . _hubConnection
25
+ . start ( )
26
+ . catch ( ( e ) => console . error ( e ) ) ;
38
27
}
39
28
}
40
29
41
30
private createConnection ( ) : void {
42
31
this . _hubConnection = new HubConnectionBuilder ( )
43
- . withUrl ( _signalrConfig . BASE_URL )
32
+ . withUrl ( SIGNALR_CONFIG . baseUrl )
44
33
. build ( ) ;
45
34
}
46
35
36
+ private hubToastMessage (
37
+ message : string ,
38
+ title : string = SIGNALR_CONFIG . messageTitle ,
39
+ delay : number = SIGNALR_CONFIG . messageDelay
40
+ ) : void {
41
+ setTimeout ( ( ) => {
42
+ EventBus . $snotify . info ( message , title ) ;
43
+ } , delay ) ;
44
+ }
45
+
47
46
private registerOnServerEvents ( ) : void {
48
- this . _hubConnection . on ( _signalrConfig . LOGIN_USER_EVENT , ( ) => {
49
- setTimeout ( ( ) => {
50
- EventBus . $snotify . info (
51
- 'A user has logged in' ,
52
- _signalrConfig . HUB_MESSAGE_TITLE
53
- ) ;
54
- } , _signalrConfig . HUB_MESSAGE_DELAY ) ;
47
+ this . _hubConnection . on ( SIGNALR_CONFIG . events . login , ( ) => {
48
+ this . hubToastMessage ( 'A user has logged in' ) ;
55
49
} ) ;
56
50
57
- this . _hubConnection . on ( _signalrConfig . LOGOUT_USER_EVENT , ( ) => {
58
- setTimeout ( ( ) => {
59
- EventBus . $snotify . info (
60
- 'A user has logged out' ,
61
- _signalrConfig . HUB_MESSAGE_TITLE
62
- ) ;
63
- } , _signalrConfig . HUB_MESSAGE_DELAY ) ;
51
+ this . _hubConnection . on ( SIGNALR_CONFIG . events . logout , ( ) => {
52
+ this . hubToastMessage ( 'A user has logged out' ) ;
64
53
} ) ;
65
54
66
- this . _hubConnection . on ( _signalrConfig . CLOSE_EVENT , ( reason : string ) => {
67
- this . _hubConnection . stop ( ) . then ( ( ) => {
68
- setTimeout ( ( ) => {
69
- EventBus . $snotify . info (
70
- `Hub closed (${ reason } )` ,
71
- _signalrConfig . HUB_MESSAGE_TITLE
72
- ) ;
73
- } , _signalrConfig . HUB_MESSAGE_DELAY ) ;
74
- } ) ;
55
+ this . _hubConnection . on ( SIGNALR_CONFIG . events . closeConnections , ( reason : string ) => {
56
+ this . _hubConnection
57
+ . stop ( )
58
+ . then ( ( ) => {
59
+ this . hubToastMessage ( `Hub closed (${ reason } )` ) ;
60
+ } ) ;
75
61
} ) ;
76
62
}
77
63
}
0 commit comments