1
1
import * as signalR from "@aspnet/signalr" ;
2
2
import * as sianglRMessagePack from "@aspnet/signalr-protocol-msgpack" ;
3
+ import { HttpTransportType , LogLevel } from "@aspnet/signalr" ;
3
4
4
5
type DotNetType = {
5
6
invokeMethod < T > ( assemblyName : string , methodIdentifier : string , ...args : any [ ] ) : T ,
6
7
invokeMethodAsync < T > ( assemblyName : string , methodIdentifier : string , ...args : any [ ] ) : Promise < T >
7
8
}
8
9
9
- const BlazorExtensionsSignalrAssembly = 'Blazor.Extensions.SignalR' ;
10
+ type DotNetReferenceType = {
11
+ invokeMethod < T > ( methodIdentifier : string , ...args : any [ ] ) : T ,
12
+ invokeMethodAsync < T > ( methodIdentifier : string , ...args : any [ ] ) : Promise < T >
13
+ }
14
+
10
15
const DotNet : DotNetType = window [ "DotNet" ] ;
11
16
12
17
export class HubConnectionManager {
13
18
private _hubConnections : Map < string , signalR . HubConnection > = new Map < string , signalR . HubConnection > ( ) ;
14
19
private _handles : Map < string , ( payload : any ) => Promise < void > > = new Map < string , ( payload : any ) => Promise < void > > ( ) ;
15
20
16
- public CreateConnection = ( connectionId : string , httpConnectionOptions : any ) => {
21
+ public CreateConnection = ( connectionId : string , httpConnectionOptions : DotNetReferenceType ) => {
17
22
if ( ! connectionId ) throw new Error ( 'Invalid connectionId.' ) ;
18
23
if ( ! httpConnectionOptions ) throw new Error ( 'Invalid transport options.' ) ;
19
- if ( ! httpConnectionOptions . url ) throw new Error ( 'Invalid hub url.' ) ;
24
+ let url = httpConnectionOptions . invokeMethod < string > ( 'get_Url' ) ;
25
+ if ( ! url ) throw new Error ( 'Invalid hub url.' ) ;
20
26
21
27
let options : any = {
22
- logger : httpConnectionOptions . logLevel ,
23
- transport : httpConnectionOptions . transport ,
24
- logMessageContent : httpConnectionOptions . logMessageContent ,
25
- skipNegotiation : httpConnectionOptions . skipNegotiation
28
+ logger : httpConnectionOptions . invokeMethod < LogLevel > ( 'get_LogLevel' ) ,
29
+ transport : httpConnectionOptions . invokeMethod < HttpTransportType > ( 'get_Transport' ) ,
30
+ logMessageContent : httpConnectionOptions . invokeMethod < boolean > ( 'get_LogMessageContent' ) ,
31
+ skipNegotiation : httpConnectionOptions . invokeMethod < boolean > ( 'get_SkipNegotiation' )
26
32
} ;
27
33
28
- if ( httpConnectionOptions . hasAccessTokenFactory ) {
34
+ if ( httpConnectionOptions . invokeMethod < true > ( 'HasAccessTokenFactory' ) ) {
29
35
options . accessTokenFactory = ( ) => {
30
36
return new Promise < string > ( async ( resolve , reject ) => {
31
- const token = await DotNet . invokeMethodAsync < string > ( BlazorExtensionsSignalrAssembly , 'GetAccessToken' , connectionId ) ;
32
-
37
+ const token = await httpConnectionOptions . invokeMethodAsync < string > ( 'GetAccessToken' ) ;
33
38
if ( token ) {
34
39
resolve ( token ) ;
35
40
} else {
@@ -42,9 +47,9 @@ export class HubConnectionManager {
42
47
if ( this . _hubConnections [ connectionId ] ) return ;
43
48
44
49
let connectionBuilder = new signalR . HubConnectionBuilder ( )
45
- . withUrl ( httpConnectionOptions . url , options ) ;
50
+ . withUrl ( url , options ) ;
46
51
47
- if ( httpConnectionOptions . addMessagePack ) {
52
+ if ( httpConnectionOptions . invokeMethod < true > ( 'get_EnableMessagePack' ) ) {
48
53
connectionBuilder
49
54
. withHubProtocol ( new sianglRMessagePack . MessagePackHubProtocol ( ) ) ;
50
55
}
@@ -58,11 +63,7 @@ export class HubConnectionManager {
58
63
59
64
public StartConnection = ( connectionId : string ) : Promise < void > => {
60
65
const connection = this . GetConnection ( connectionId ) ;
61
-
62
- connection . onclose ( async err => {
63
- await DotNet . invokeMethodAsync ( BlazorExtensionsSignalrAssembly , 'OnClose' , connectionId , JSON . stringify ( err ) ) ;
64
- } ) ;
65
-
66
+
66
67
return connection . start ( ) ;
67
68
}
68
69
@@ -90,14 +91,14 @@ export class HubConnectionManager {
90
91
return connection ;
91
92
}
92
93
93
- private On = ( connectionId : string , methodName : string , handleId : string ) => {
94
+ public On = ( connectionId : string , callback : DotNetReferenceType ) => {
94
95
const connection = this . GetConnection ( connectionId ) ;
95
- const handle = ( payload ) => this . OnHandler ( connectionId , methodName , handleId , payload ) ;
96
- this . _handles . set ( handleId , handle ) ;
97
- connection . on ( methodName , handle ) ;
96
+ const handle = ( payload ) => callback . invokeMethodAsync < void > ( 'On' , JSON . stringify ( payload ) ) ;
97
+ this . _handles . set ( callback . invokeMethod < string > ( 'get_Id' ) , handle ) ;
98
+ connection . on ( callback . invokeMethod < string > ( 'get_MethodName' ) , handle ) ;
98
99
}
99
100
100
- private Off = ( connectionId : string , methodName : string , handleId : string ) => {
101
+ public Off = ( connectionId : string , methodName : string , handleId : string ) => {
101
102
const connection = this . GetConnection ( connectionId ) ;
102
103
const handle = this . _handles . get ( handleId ) ;
103
104
if ( handle ) {
@@ -106,38 +107,10 @@ export class HubConnectionManager {
106
107
}
107
108
}
108
109
109
- private OnHandler = async ( connectionId : string , methodName : string , handleId : string , payload : any ) => {
110
- await DotNet . invokeMethodAsync ( BlazorExtensionsSignalrAssembly , 'Dispatch' , connectionId , methodName , handleId , JSON . stringify ( payload ) ) ;
111
- }
112
-
113
- public static initialize ( ) {
114
- //const Blazor: BlazorType = window["Blazor"];
115
- //window["BlazorExtensions"].HubConnectionManager = new HubConnectionManager();
116
-
117
- //Blazor.registerFunction('Blazor.Extensions.SignalR.InvokeAsync', (connectionId: string, methodName: string, args: any) => {
118
- // //TODO remove this parse after Blazor fixed the async args json parsing code
119
- // const parsedConnectionId = JSON.parse(connectionId);
120
- // const parsedMethodName = JSON.parse(methodName);
121
- // const parsedArgs = JSON.parse(args);
122
-
123
- // return window["BlazorExtensions"].HubConnectionManager.invokeAsync(parsedConnectionId, parsedMethodName, ...parsedArgs);
124
- //});
125
-
126
- //Blazor.registerFunction('Blazor.Extensions.SignalR.InvokeWithResultAsync', (connectionId: string, methodName: string, args: any) => {
127
- // //TODO remove this parse after Blazor fixed the async args json parsing code
128
- // const parsedConnectionId = JSON.parse(connectionId);
129
- // const parsedMethodName = JSON.parse(methodName);
130
- // const parsedArgs = JSON.parse(args);
131
-
132
- // return window["BlazorExtensions"].HubConnectionManager.invokeWithResultAsync(parsedConnectionId, parsedMethodName, ...parsedArgs);
133
- //});
134
-
135
- //Blazor.registerFunction('Blazor.Extensions.SignalR.On', (connectionId: string, methodName: string, handleId: string) => {
136
- // return window["BlazorExtensions"].HubConnectionManager.on(connectionId, methodName, handleId);
137
- //});
138
-
139
- //Blazor.registerFunction('Blazor.Extensions.SignalR.Off', (connectionId: string, methodName: string, handleId: string) => {
140
- // return window["BlazorExtensions"].HubConnectionManager.off(connectionId, methodName, handleId);
141
- //});
110
+ public OnClose = ( connectionId : string , onErrorCallback : DotNetReferenceType ) => {
111
+ const connection = this . GetConnection ( connectionId ) ;
112
+ connection . onclose ( async err => {
113
+ onErrorCallback . invokeMethodAsync < void > ( 'OnClose' , JSON . stringify ( err ) ) ;
114
+ } ) ;
142
115
}
143
116
}
0 commit comments