@@ -42,15 +42,33 @@ class SockJsClient extends React.Component {
4242 }
4343
4444 componentWillUnmount ( ) {
45- this . subscriptions . forEach ( ( subid , topic ) => {
46- this . unsubscribe ( topic ) ;
47- } ) ;
45+ this . disconnect ( ) ;
4846 }
4947
5048 render ( ) {
5149 return ( < div > </ div > ) ;
5250 }
5351
52+ _initStompClient = ( ) => {
53+ // Websocket held by stompjs can be opened only once
54+ this . client = Stomp . over ( new SockJS ( this . props . url ) ) ;
55+ if ( ! this . props . debug ) {
56+ this . client . debug = ( ) => { } ;
57+ }
58+ }
59+
60+ _cleanUp = ( ) => {
61+ this . setState ( { connected : false } ) ;
62+ this . retryCount = 0 ;
63+ this . subscriptions . clear ( ) ;
64+ }
65+
66+ _log = ( msg ) => {
67+ if ( this . props . debug ) {
68+ console . log ( msg ) ;
69+ }
70+ }
71+
5472 connect = ( ) => {
5573 this . _initStompClient ( ) ;
5674 this . client . connect ( this . props . headers , ( ) => {
@@ -66,23 +84,26 @@ class SockJsClient extends React.Component {
6684 this . props . onDisconnect ( ) ;
6785 }
6886 if ( this . props . autoReconnect ) {
69- setTimeout ( this . connect , this . props . getRetryInterval ( this . retryCount ++ ) ) ;
87+ this . _timeoutId = setTimeout ( this . connect , this . props . getRetryInterval ( this . retryCount ++ ) ) ;
7088 }
7189 } ) ;
7290 }
7391
74- _initStompClient = ( ) => {
75- // Websocket held by stompjs can be opened only once
76- this . client = Stomp . over ( new SockJS ( this . props . url ) ) ;
77- if ( ! this . props . debug ) {
78- this . client . debug = ( ) => { } ;
92+ disconnect = ( ) => {
93+ // On calling disconnect explicitly no effort will be made to reconnect
94+ // Clear timeoutId in case the component is trying to reconnect
95+ if ( this . _timeoutId ) {
96+ clearTimeout ( this . _timeoutId ) ;
97+ }
98+ if ( this . state . connected ) {
99+ this . subscriptions . forEach ( ( subid , topic ) => {
100+ this . unsubscribe ( topic ) ;
101+ } ) ;
102+ this . client . disconnect ( ( ) => {
103+ this . _cleanUp ( ) ;
104+ this . _log ( "Stomp client is successfully disconnected!" ) ;
105+ } ) ;
79106 }
80- }
81-
82- _cleanUp = ( ) => {
83- this . setState ( { connected : false } ) ;
84- this . retryCount = 0 ;
85- this . subscriptions . clear ( ) ;
86107 }
87108
88109 subscribe = ( topic ) => {
0 commit comments