|
1 |
| -import * as signalR from "@microsoft/signalr"; |
2 |
| -import * as sianglRMessagePack from "@microsoft/signalr-protocol-msgpack"; |
3 |
| -import { HttpTransportType, LogLevel } from "@microsoft/signalr"; |
4 |
| - |
5 |
| -type DotNetType = { |
6 |
| - invokeMethod<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): T, |
7 |
| - invokeMethodAsync<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): Promise<T> |
8 |
| -} |
9 |
| - |
10 |
| -type DotNetReferenceType = { |
11 |
| - invokeMethod<T>(methodIdentifier: string, ...args: any[]): T, |
12 |
| - invokeMethodAsync<T>(methodIdentifier: string, ...args: any[]): Promise<T> |
13 |
| -} |
14 |
| - |
15 |
| -export class HubConnectionManager { |
16 |
| - private _hubConnections: Map<string, signalR.HubConnection> = new Map<string, signalR.HubConnection>(); |
17 |
| - private _handles: Map<string, (payload: any) => Promise<void>> = new Map<string, (payload: any) => Promise<void>>(); |
18 |
| - |
19 |
| - public CreateConnection = (connectionId: string, httpConnectionOptions: DotNetReferenceType) => { |
20 |
| - if (!connectionId) throw new Error('Invalid connectionId.'); |
21 |
| - if (!httpConnectionOptions) throw new Error('Invalid transport options.'); |
22 |
| - |
23 |
| - const url = httpConnectionOptions.invokeMethod<string>('get_Url'); |
24 |
| - |
25 |
| - if (!url) throw new Error('Invalid hub url.'); |
26 |
| - |
27 |
| - let options: any = { |
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') |
32 |
| - }; |
33 |
| - |
34 |
| - if (httpConnectionOptions.invokeMethod<true>('HasAccessTokenFactory')) { |
35 |
| - options.accessTokenFactory = () => { |
36 |
| - return new Promise<string>(async (resolve, reject) => { |
37 |
| - const token = await httpConnectionOptions.invokeMethodAsync<string>('GetAccessToken'); |
38 |
| - if (token) { |
39 |
| - resolve(token); |
40 |
| - } else { |
41 |
| - reject(); |
42 |
| - } |
43 |
| - }) |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - if (this._hubConnections[connectionId]) return; |
48 |
| - |
49 |
| - let connectionBuilder = new signalR.HubConnectionBuilder() |
50 |
| - .withUrl(url, options); |
51 |
| - |
52 |
| - if (httpConnectionOptions.invokeMethod<true>('get_EnableMessagePack')) { |
53 |
| - connectionBuilder |
54 |
| - .withHubProtocol(new sianglRMessagePack.MessagePackHubProtocol()); |
55 |
| - } |
56 |
| - |
57 |
| - this._hubConnections.set(connectionId, connectionBuilder.build()); |
58 |
| - } |
59 |
| - |
60 |
| - public RemoveConnection = (connectionId: string) => { |
61 |
| - this._hubConnections.delete(connectionId); |
62 |
| - } |
63 |
| - |
64 |
| - public StartConnection = (connectionId: string): Promise<void> => { |
65 |
| - const connection = this.GetConnection(connectionId); |
66 |
| - |
67 |
| - return connection.start(); |
68 |
| - } |
69 |
| - |
70 |
| - public StopConnection = (connectionId: string): Promise<void> => { |
71 |
| - const connection = this.GetConnection(connectionId); |
72 |
| - |
73 |
| - return connection.stop(); |
74 |
| - } |
75 |
| - |
76 |
| - public InvokeAsync = (connectionId: string, methodName: string, args: any[]): Promise<void> => { |
77 |
| - const connection = this.GetConnection(connectionId); |
78 |
| - |
79 |
| - return connection.invoke(methodName, ...args); |
80 |
| - } |
81 |
| - |
82 |
| - public InvokeWithResultAsync = async (connectionId: string, methodName: string, args: any[]): Promise<any> => { |
83 |
| - const connection = this.GetConnection(connectionId); |
84 |
| - |
85 |
| - var result = await connection.invoke(methodName, ...args); |
86 |
| - |
87 |
| - return this.ReplaceTypedArray(result); |
88 |
| - } |
89 |
| - |
90 |
| - private GetConnection = (connectionId: string) => { |
91 |
| - if (!connectionId) throw new Error('Invalid connectionId.'); |
92 |
| - |
93 |
| - const connection = this._hubConnections.get(connectionId); |
94 |
| - |
95 |
| - if (!connection) throw new Error('Invalid connection.'); |
96 |
| - |
97 |
| - return connection; |
98 |
| - } |
99 |
| - |
100 |
| - private ReplaceTypedArray = (obj: any): any => { |
101 |
| - if (obj instanceof Int8Array || |
102 |
| - obj instanceof Uint8Array || |
103 |
| - obj instanceof Uint8ClampedArray || |
104 |
| - obj instanceof Int16Array || |
105 |
| - obj instanceof Uint16Array || |
106 |
| - obj instanceof Int32Array || |
107 |
| - obj instanceof Uint32Array || |
108 |
| - obj instanceof Float32Array || |
109 |
| - obj instanceof Float64Array) |
110 |
| - { |
111 |
| - obj = Array.prototype.slice.call(obj); |
112 |
| - } |
113 |
| - |
114 |
| - return obj; |
115 |
| - } |
116 |
| - |
117 |
| - public On = (connectionId: string, callback: DotNetReferenceType) => { |
118 |
| - const connection = this.GetConnection(connectionId); |
119 |
| - const handle = (...payloads) => callback.invokeMethodAsync<void>( |
120 |
| - 'On', payloads.map(payload => JSON.stringify(this.ReplaceTypedArray(payload)))); |
121 |
| - |
122 |
| - this._handles.set(callback.invokeMethod<string>('get_Id'), handle); |
123 |
| - |
124 |
| - connection.on(callback.invokeMethod<string>('get_MethodName'), handle); |
125 |
| - } |
126 |
| - |
127 |
| - public Off = (connectionId: string, methodName: string, handleId: string) => { |
128 |
| - const connection = this.GetConnection(connectionId); |
129 |
| - const handle = this._handles.get(handleId); |
130 |
| - |
131 |
| - if (handle) { |
132 |
| - connection.off(methodName, handle); |
133 |
| - |
134 |
| - this._handles.delete(handleId); |
135 |
| - } |
136 |
| - } |
137 |
| - |
138 |
| - public OnClose = (connectionId: string, onErrorCallback: DotNetReferenceType) => { |
139 |
| - const connection = this.GetConnection(connectionId); |
140 |
| - |
141 |
| - connection.onclose(async err => { |
142 |
| - onErrorCallback.invokeMethodAsync<void>('OnClose', JSON.stringify(err)); |
143 |
| - }); |
144 |
| - } |
145 |
| -} |
| 1 | +import * as signalR from "@microsoft/signalr"; |
| 2 | +import * as sianglRMessagePack from "@microsoft/signalr-protocol-msgpack"; |
| 3 | +import { HttpTransportType, LogLevel } from "@microsoft/signalr"; |
| 4 | + |
| 5 | +type DotNetType = { |
| 6 | + invokeMethod<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): T, |
| 7 | + invokeMethodAsync<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): Promise<T> |
| 8 | +} |
| 9 | + |
| 10 | +type DotNetReferenceType = { |
| 11 | + invokeMethod<T>(methodIdentifier: string, ...args: any[]): T, |
| 12 | + invokeMethodAsync<T>(methodIdentifier: string, ...args: any[]): Promise<T> |
| 13 | +} |
| 14 | + |
| 15 | +export class HubConnectionManager { |
| 16 | + private _hubConnections: Map<string, signalR.HubConnection> = new Map<string, signalR.HubConnection>(); |
| 17 | + private _handles: Map<string, (payload: any) => Promise<void>> = new Map<string, (payload: any) => Promise<void>>(); |
| 18 | + |
| 19 | + public CreateConnection = (connectionId: string, httpConnectionOptions: DotNetReferenceType) => { |
| 20 | + if (!connectionId) throw new Error('Invalid connectionId.'); |
| 21 | + if (!httpConnectionOptions) throw new Error('Invalid transport options.'); |
| 22 | + |
| 23 | + const url = httpConnectionOptions.invokeMethod<string>('get_Url'); |
| 24 | + |
| 25 | + if (!url) throw new Error('Invalid hub url.'); |
| 26 | + |
| 27 | + let options: any = { |
| 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') |
| 32 | + }; |
| 33 | + |
| 34 | + if (httpConnectionOptions.invokeMethod<boolean>('HasAccessTokenFactory')) { |
| 35 | + options.accessTokenFactory = () => { |
| 36 | + return new Promise<string>(async (resolve, reject) => { |
| 37 | + const token = await httpConnectionOptions.invokeMethodAsync<string>('GetAccessToken'); |
| 38 | + if (token) { |
| 39 | + resolve(token); |
| 40 | + } else { |
| 41 | + reject(); |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (this._hubConnections[connectionId]) return; |
| 48 | + |
| 49 | + let connectionBuilder = new signalR.HubConnectionBuilder() |
| 50 | + .withUrl(url, options); |
| 51 | + |
| 52 | + if (httpConnectionOptions.invokeMethod<boolean>('get_EnableMessagePack')) { |
| 53 | + connectionBuilder |
| 54 | + .withHubProtocol(new sianglRMessagePack.MessagePackHubProtocol()); |
| 55 | + } |
| 56 | + |
| 57 | + this._hubConnections.set(connectionId, connectionBuilder.build()); |
| 58 | + } |
| 59 | + |
| 60 | + public RemoveConnection = (connectionId: string) => { |
| 61 | + this._hubConnections.delete(connectionId); |
| 62 | + } |
| 63 | + |
| 64 | + public StartConnection = (connectionId: string): Promise<void> => { |
| 65 | + const connection = this.GetConnection(connectionId); |
| 66 | + |
| 67 | + return connection.start(); |
| 68 | + } |
| 69 | + |
| 70 | + public StopConnection = (connectionId: string): Promise<void> => { |
| 71 | + const connection = this.GetConnection(connectionId); |
| 72 | + |
| 73 | + return connection.stop(); |
| 74 | + } |
| 75 | + |
| 76 | + public InvokeAsync = (connectionId: string, methodName: string, args: any[]): Promise<void> => { |
| 77 | + const connection = this.GetConnection(connectionId); |
| 78 | + |
| 79 | + return connection.invoke(methodName, ...args); |
| 80 | + } |
| 81 | + |
| 82 | + public InvokeWithResultAsync = async (connectionId: string, methodName: string, args: any[]): Promise<any> => { |
| 83 | + const connection = this.GetConnection(connectionId); |
| 84 | + |
| 85 | + var result = await connection.invoke(methodName, ...args); |
| 86 | + |
| 87 | + return this.ReplaceTypedArray(result); |
| 88 | + } |
| 89 | + |
| 90 | + private GetConnection = (connectionId: string) => { |
| 91 | + if (!connectionId) throw new Error('Invalid connectionId.'); |
| 92 | + |
| 93 | + const connection = this._hubConnections.get(connectionId); |
| 94 | + |
| 95 | + if (!connection) throw new Error('Invalid connection.'); |
| 96 | + |
| 97 | + return connection; |
| 98 | + } |
| 99 | + |
| 100 | + private ReplaceTypedArray = (obj: any): any => { |
| 101 | + if (obj instanceof Int8Array || |
| 102 | + obj instanceof Uint8Array || |
| 103 | + obj instanceof Uint8ClampedArray || |
| 104 | + obj instanceof Int16Array || |
| 105 | + obj instanceof Uint16Array || |
| 106 | + obj instanceof Int32Array || |
| 107 | + obj instanceof Uint32Array || |
| 108 | + obj instanceof Float32Array || |
| 109 | + obj instanceof Float64Array) |
| 110 | + { |
| 111 | + obj = Array.prototype.slice.call(obj); |
| 112 | + } |
| 113 | + |
| 114 | + return obj; |
| 115 | + } |
| 116 | + |
| 117 | + public On = (connectionId: string, callback: DotNetReferenceType) => { |
| 118 | + const connection = this.GetConnection(connectionId); |
| 119 | + const handle = (...payloads) => callback.invokeMethodAsync<void>( |
| 120 | + 'On', payloads.map(payload => JSON.stringify(this.ReplaceTypedArray(payload)))); |
| 121 | + |
| 122 | + this._handles.set(callback.invokeMethod<string>('get_Id'), handle); |
| 123 | + |
| 124 | + connection.on(callback.invokeMethod<string>('get_MethodName'), handle); |
| 125 | + } |
| 126 | + |
| 127 | + public Off = (connectionId: string, methodName: string, handleId: string) => { |
| 128 | + const connection = this.GetConnection(connectionId); |
| 129 | + const handle = this._handles.get(handleId); |
| 130 | + |
| 131 | + if (handle) { |
| 132 | + connection.off(methodName, handle); |
| 133 | + |
| 134 | + this._handles.delete(handleId); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public OnClose = (connectionId: string, onErrorCallback: DotNetReferenceType) => { |
| 139 | + const connection = this.GetConnection(connectionId); |
| 140 | + |
| 141 | + connection.onclose(async err => { |
| 142 | + onErrorCallback.invokeMethodAsync<void>('OnClose', JSON.stringify(err)); |
| 143 | + }); |
| 144 | + } |
| 145 | +} |
0 commit comments