@@ -66,6 +66,9 @@ public class KiteTicker {
6666 private int nextReconnectInterval = 0 ;
6767 private int maxRetryInterval = 30000 ;
6868 private Map <Long , String > modeMap ;
69+ private Timer canReconnectTimer = null ;
70+ /** Used to reconnect after the specified delay.*/
71+ private boolean canReconnect = true ;
6972
7073 /** Initialize Kite Ticker.
7174 * @param accessToken is the token received after successful login process.
@@ -98,7 +101,6 @@ public void run() {
98101
99102 Date currentDate = new Date ();
100103 long timeInterval = (currentDate .getTime () - lastPongAt );
101-
102104 if (timeInterval >= 2 * pingInterval ) {
103105 doReconnect ();
104106 }
@@ -120,16 +122,25 @@ public void doReconnect() {
120122 if (nextReconnectInterval > maxRetryInterval ){
121123 nextReconnectInterval = maxRetryInterval ;
122124 }
123-
124- count ++;
125- if (count < maxRetries ) {
126- Timer timer = new Timer ();
127- timer .schedule (new TimerTask () {
128- @ Override
129- public void run () {
130- reconnect (new ArrayList <>(subscribedTokens ));
131- }
132- }, nextReconnectInterval );
125+ if (count <= maxRetries ) {
126+ if (canReconnect ) {
127+ count ++;
128+ reconnect (new ArrayList <>(subscribedTokens ));
129+ canReconnect = false ;
130+ canReconnectTimer = new Timer ();
131+ canReconnectTimer .schedule (new TimerTask () {
132+ @ Override
133+ public void run () {
134+ canReconnect = true ;
135+ }
136+ }, nextReconnectInterval );
137+ }
138+ }else if (count > maxRetries ) {
139+ // if number of tries exceeds maximum number of retries then stop timer.
140+ if (timer != null ) {
141+ timer .cancel ();
142+ timer = null ;
143+ }
133144 }
134145 }
135146
@@ -160,7 +171,8 @@ public void setMaximumRetries(int maxRetries) throws KiteException {
160171 /* Set a maximum interval for every retry.*/
161172 public void setMaximumRetryInterval (int interval ) throws KiteException {
162173 if (interval >= 5 ) {
163- maxRetryInterval = interval ;
174+ //convert to milliseconds
175+ maxRetryInterval = interval * 1000 ;
164176 } else {
165177 throw new KiteException ("Maximum retry interval can't be less than 0" );
166178 }
@@ -205,7 +217,16 @@ public void connect() {
205217 if (onErrorListener != null ) {
206218 onErrorListener .onError (e );
207219 }
208- doReconnect ();
220+ if (tryReconnection ) {
221+ if (timer == null ) {
222+ // this is to handle reconnection first time
223+ if (lastPongAt == 0 ) {
224+ lastPongAt = 1 ;
225+ }
226+ timer = new Timer ();
227+ timer .scheduleAtFixedRate (getTask (), 0 , pongCheckInterval );
228+ }
229+ }
209230 }
210231 }
211232
@@ -216,7 +237,6 @@ public WebSocketAdapter getWebsocketAdapter(){
216237 @ Override
217238 public void onConnected (WebSocket websocket , Map <String , List <String >> headers ) {
218239 count = 0 ;
219-
220240 nextReconnectInterval = 0 ;
221241
222242 if (onConnectedListener != null ) {
@@ -281,13 +301,9 @@ public void onPongFrame(WebSocket websocket, WebSocketFrame frame) {
281301 */
282302 @ Override
283303 public void onDisconnected (WebSocket websocket , WebSocketFrame serverCloseFrame , WebSocketFrame clientCloseFrame , boolean closedByServer ) {
284- if (timer != null ){
285- timer .cancel ();
286- }
287304 if (onDisconnectedListener != null ) {
288305 onDisconnectedListener .onDisconnected ();
289306 }
290- //connection = false;
291307 return ;
292308 }
293309
@@ -627,7 +643,6 @@ private void reconnect(final ArrayList<Long> tokens) {
627643 return ;
628644 }
629645 ws .addListener (getWebsocketAdapter ());
630- lastPongAt = 0 ;
631646 connect ();
632647 final OnConnect onUsersConnectedListener = this .onConnectedListener ;
633648 setOnConnectedListener (new OnConnect () {
@@ -652,6 +667,7 @@ public void onConnected() {
652667 setMode (modeArrayItem .getValue (), modeArrayItem .getKey ());
653668 }
654669 }
670+ lastPongAt = 0 ;
655671 count = 0 ;
656672 nextReconnectInterval = 0 ;
657673 onConnectedListener = onUsersConnectedListener ;
0 commit comments