1- import * as signalR from "@aspnet/signalr" ;
1+ import * as SignalR from "@aspnet/signalr" ;
22import { AppEvents } from './app.common' ;
33
4- var storeFunc = null ;
54class SignalRApp {
65 constructor ( url ) {
76 this . url = url ;
87 this . connection = null ;
9- this . isConnected = false ;
8+ this . isConnected = false ;
9+ this . processResponse = null ;
1010 }
1111
1212 Init ( options ) {
@@ -15,22 +15,23 @@ class SignalRApp {
1515 options . accessTokenFactory = ( ) => options . getToken ( ) ;
1616 }
1717
18- this . connection = new signalR . HubConnectionBuilder ( )
18+ this . connection = new SignalR . HubConnectionBuilder ( )
1919 . withUrl ( options . url , options )
20- . configureLogging ( signalR . LogLevel . Information )
20+ . configureLogging ( SignalR . LogLevel . Information )
2121 . build ( ) ;
22-
2322
2423 //Receive Data
2524 //Reading the raw response
2625 var self = this ;
27- storeFunc = self . connection . processIncomingData ;
26+ self . processResponse = self . connection . processIncomingData ;
27+
2828 self . connection . processIncomingData = function ( data ) {
29- console . log ( 'processIncomingData' + data ) ;
30- storeFunc . call ( self . connection , data ) ;
29+ self . processResponse . call ( self . connection , data ) ;
30+ self . HandleResponse ( data ) ;
3131 }
32- debugger ;
32+
3333 AppEvents . emit ( 'Init' , options ) ;
34+ AppEvents . emit ( 'Logger' , "Init" ) ;
3435 }
3536
3637
@@ -41,6 +42,7 @@ class SignalRApp {
4142 AppEvents . emit ( 'OnConnect' , { url : self . url } ) ;
4243 } )
4344 . catch ( function ( err ) {
45+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
4446 return console . error ( err . toString ( ) ) ;
4547 } ) ;
4648 }
@@ -50,28 +52,68 @@ class SignalRApp {
5052 methodArguments = options . methodArguments ;
5153
5254 AppEvents . emit ( 'OnSend' , options ) ;
55+ AppEvents . emit ( 'Logger' , "Calling... " + options . methodName ) ;
5356 this . connection . invoke ( options . methodName , ...methodArguments )
5457 . catch ( function ( err ) {
58+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
5559 return console . log ( err ) ;
5660 } ) ;
5761 }
5862
5963 OnReceive ( callback ) {
60- this . connection . on ( "ReceiveData" , function ( data ) {
61- callback ( data ) ;
62- } ) ;
64+ // this.connection.on("ReceiveData", function (data) {
65+ // callback(data);
66+ // });
6367 }
6468
6569 OnDisConnect ( ) {
6670 this . connection . stop ( )
6771 . then ( function ( ) {
6872 console . log ( 'Disconnected' ) ;
73+ AppEvents . emit ( 'Logger' , "Disconnected..." ) ;
6974 AppEvents . emit ( 'OnDisconnected' ) ;
7075 } )
7176 . catch ( function ( err ) {
77+ AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
7278 return console . error ( err . toString ( ) ) ;
7379 } ) ;
7480 }
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+ }
75117}
76118
77119export { SignalRApp }
0 commit comments