@@ -11,12 +11,33 @@ class SignalRApp {
1111
1212 Init ( options ) {
1313
14+ var confguration = { } ;
1415 if ( options . isTokenRequired === true ) {
15- options . accessTokenFactory = ( ) => options . getToken ( ) ;
16+ // options.skipNegotiation = true;
17+ confguration . accessTokenFactory = ( ) => options . getToken ( ) ;
1618 }
17-
19+
20+ switch ( options . transportType ) {
21+ case "ws" :
22+ confguration . transport = SignalR . HttpTransportType . WebSockets ;
23+ break ;
24+ case "lp" :
25+ confguration . transport = SignalR . HttpTransportType . LongPolling ;
26+ break ;
27+ case "sse" :
28+ confguration . transport = SignalR . HttpTransportType . ServerSentEvents ;
29+ break ;
30+ default :
31+ confguration . transport = SignalR . HttpTransportType . WebSockets ;
32+ }
33+
34+ //confguration.logMessageContent = true;
35+ if ( ! ! options . skipNegotiation ) {
36+ confguration . skipNegotiation = options . skipNegotiation ;
37+ }
38+
1839 this . connection = new SignalR . HubConnectionBuilder ( )
19- . withUrl ( options . url , options )
40+ . withUrl ( options . url , confguration )
2041 . configureLogging ( SignalR . LogLevel . Information )
2142 . withAutomaticReconnect ( [ 0 , 3000 , 5000 , 10000 , 15000 , 30000 ] )
2243 . build ( ) ;
@@ -32,69 +53,49 @@ class SignalRApp {
3253 }
3354
3455 self . connection . onreconnecting ( ( error ) => {
35- // disableUi(true);
36- // const li = document.createElement("li");
37- // li.textContent = `Connection lost due to error "${error}". Reconnecting.`;
38- // document.getElementById("messagesList").appendChild(li);
3956 AppEvents . emit ( 'Logger' , `Connection lost due to error "${ error } ". Reconnecting.` ) ;
4057 console . log ( 'On Reconnecting...' ) ;
4158 } ) ;
4259
4360 self . connection . onreconnected ( ( connectionId ) => {
44- // disableUi(false);
45- // const li = document.createElement("li");
46- // li.textContent = `Connection reestablished. Connected.`;
47- // document.getElementById("messagesList").appendChild(li);
4861 AppEvents . emit ( 'Logger' , `Connection reestablished. Connected` ) ;
4962 console . log ( 'On Reconnected...' ) ;
5063 } ) ;
51-
52- AppEvents . emit ( 'Init' , options ) ;
53- AppEvents . emit ( 'Logger' , "Init" ) ;
5464 }
5565
5666
57- OnConnect ( ) {
67+ OnConnect ( onSuccess , onError ) {
5868 var self = this ;
5969 self . connection . start ( )
60- . then ( function ( ) {
61- AppEvents . emit ( 'OnConnected' , { url : self . url } ) ;
70+ . then ( function ( data ) {
71+ onSuccess ( { url : self . url } ) ;
6272 } )
6373 . catch ( function ( err ) {
64- AppEvents . emit ( 'ConnectionFailed' , err . toString ( ) ) ;
65- return console . error ( err . toString ( ) ) ;
74+ onError ( err )
6675 } ) ;
6776 }
6877
69- OnSend ( options ) {
78+ OnSend ( options , beforeInvoke , onError ) {
7079 var methodArguments = new Array ( ) ;
7180 methodArguments = options . methodArguments ;
7281
73- AppEvents . emit ( 'OnSend' , options ) ;
74- AppEvents . emit ( 'Logger' , "Calling... " + options . methodName ) ;
82+ beforeInvoke ( options ) ;
7583 this . connection . invoke ( options . methodName , ...methodArguments )
7684 . catch ( function ( err ) {
77- AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
78- return console . log ( err ) ;
85+ onError ( err ) ;
7986 } ) ;
8087 }
8188
8289 OnReceive ( callback ) {
83- // this.connection.on("ReceiveData", function (data) {
84- // callback(data);
85- // });
8690 }
8791
88- OnDisConnect ( ) {
92+ OnDisconnect ( onSuccess , onError ) {
8993 this . connection . stop ( )
9094 . then ( function ( ) {
91- console . log ( 'Disconnected' ) ;
92- AppEvents . emit ( 'Logger' , "Disconnected..." ) ;
93- AppEvents . emit ( 'OnDisconnected' ) ;
95+ onSuccess ( ) ;
9496 } )
9597 . catch ( function ( err ) {
96- AppEvents . emit ( 'Logger' , err . toString ( ) ) ;
97- return console . error ( err . toString ( ) ) ;
98+ onError ( err ) ;
9899 } ) ;
99100 }
100101
0 commit comments