@@ -35,6 +35,7 @@ public class WebSocketClient implements WebSocket.Listener {
3535 private final Consumer <CharSequence > onTextCallback ;
3636
3737 private final AtomicBoolean attemptReconnect = new AtomicBoolean ();
38+ private final AtomicBoolean keepPinging = new AtomicBoolean ();
3839 private CountDownLatch pingCountdownLatch ;
3940
4041 /**
@@ -90,6 +91,7 @@ public void onOpen(WebSocket webSocket) {
9091 connectCallback .run ();
9192 }
9293 logger .log (Level .INFO , "Connected to " + uri );
94+ keepPinging .set (true );
9395 new Thread (new PingRunnable ()).start ();
9496 }
9597
@@ -134,7 +136,7 @@ public CompletionStage<?> onClose(WebSocket webSocket,
134136 * is called.
135137 */
136138 public void sendPing () {
137- logger .log (Level .FINE , " Sending ping" );
139+ logger .log (Level .FINE , Thread . currentThread (). getName () + " Sending ping" );
138140 webSocket .sendPing (ByteBuffer .allocate (0 ));
139141 }
140142
@@ -175,6 +177,7 @@ public CompletionStage<?> onText(WebSocket webSocket,
175177 * @param reason Custom reason text.
176178 */
177179 public void close (String reason ) {
180+ keepPinging .set (false );
178181 webSocket .sendClose (1000 , reason );
179182 }
180183
@@ -197,7 +200,7 @@ private class PingRunnable implements Runnable {
197200
198201 @ Override
199202 public void run () {
200- while (true ) {
203+ while (keepPinging . get () ) {
201204 pingCountdownLatch = new CountDownLatch (1 );
202205 sendPing ();
203206 try {
0 commit comments