1+ import * as SignalR from "@aspnet/signalr" ;
2+ import { AppEvents } from './app.common' ;
3+
4+ class SignalRApp {
5+ constructor ( url ) {
6+ this . url = url ;
7+ this . connection = null ;
8+ this . isConnected = false ;
9+ this . processResponse = null ;
10+ }
11+
12+ Init ( options ) {
13+
14+ if ( options . isTokenRequired === true ) {
15+ options . accessTokenFactory = ( ) => options . getToken ( ) ;
16+ }
17+
18+ this . connection = new SignalR . HubConnectionBuilder ( )
19+ . withUrl ( options . url , options )
20+ . configureLogging ( SignalR . LogLevel . Information )
21+ . build ( ) ;
22+
23+ //Receive Data
24+ //Reading the raw response
25+ var self = this ;
26+ self . processResponse = self . connection . processIncomingData ;
27+
28+ self . connection . processIncomingData = function ( data ) {
29+ self . processResponse . call ( self . connection , data ) ;
30+ self . HandleResponse ( data ) ;
31+ }
32+
33+ AppEvents . emit ( 'Init' , options ) ;
34+ AppEvents . emit ( 'Logger' , "Init" ) ;
35+ }
36+
37+
38+ OnConnect ( ) {
39+ var self = this ;
40+ self . connection . start ( )
41+ . then ( function ( ) {
42+ AppEvents . emit ( 'OnConnect' , { url : self . url } ) ;
43+ } )
44+ . catch ( function ( err ) {
45+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
46+ return console . error ( err . toString ( ) ) ;
47+ } ) ;
48+ }
49+
50+ OnSend ( options ) {
51+ var methodArguments = new Array ( ) ;
52+ methodArguments = options . methodArguments ;
53+
54+ AppEvents . emit ( 'OnSend' , options ) ;
55+ AppEvents . emit ( 'Logger' , "Calling... " + options . methodName ) ;
56+ this . connection . invoke ( options . methodName , ...methodArguments )
57+ . catch ( function ( err ) {
58+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
59+ return console . log ( err ) ;
60+ } ) ;
61+ }
62+
63+ OnReceive ( callback ) {
64+ // this.connection.on("ReceiveData", function (data) {
65+ // callback(data);
66+ // });
67+ }
68+
69+ OnDisConnect ( ) {
70+ this . connection . stop ( )
71+ . then ( function ( ) {
72+ console . log ( 'Disconnected' ) ;
73+ AppEvents . emit ( 'Logger' , "Disconnected..." ) ;
74+ AppEvents . emit ( 'OnDisconnected' ) ;
75+ } )
76+ . catch ( function ( err ) {
77+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
78+ return console . error ( err . toString ( ) ) ;
79+ } ) ;
80+ }
81+
82+ HandleResponse ( input ) {
83+ AppEvents . emit ( 'Logger' , input . toString ( ) ) ;
84+ var output = this . ParseRespose ( input ) ;
85+ if ( output !== null ) {
86+
87+ output . forEach ( ( e ) => {
88+ var jsonObj = JSON . parse ( e ) ;
89+
90+ if ( jsonObj !== null && jsonObj . hasOwnProperty ( 'target' ) ) {
91+ debugger ;
92+ AppEvents . emit ( 'ReceivedData' , { "ClientMethod" : jsonObj . target , "Data" : jsonObj . arguments } ) ;
93+ }
94+ } ) ;
95+ }
96+ }
97+
98+ ParseRespose ( input ) {
99+
100+ if ( typeof input !== "string" ) {
101+ console . log ( "Invalid input for JSON hub protocol. Expected a string." ) ;
102+ return null ;
103+ //throw new Error("Invalid input for JSON hub protocol. Expected a string.");
104+ }
105+
106+ var separator = String . fromCharCode ( 0x1e ) ;
107+ if ( input [ input . length - 1 ] !== separator ) {
108+ console . log ( "Message is incomplete." ) ;
109+ //throw new Error("Message is incomplete.");
110+ return null ;
111+ }
112+
113+ var messages = input . split ( separator ) ;
114+ messages . pop ( ) ;
115+ return messages ;
116+ }
117+ }
118+
119+ export { SignalRApp }
0 commit comments