@@ -74,10 +74,10 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
7474 private ScheduledFuture connectionLostCheckerFuture ;
7575
7676 /**
77- * Attribute for the lost connection check interval
77+ * Attribute for the lost connection check interval in nanoseconds
7878 * @since 1.3.4
7979 */
80- private int connectionLostTimeout = 60 ;
80+ private long connectionLostTimeout = TimeUnit . SECONDS . toNanos ( 60 ) ;
8181
8282 /**
8383 * Attribute to keep track if the WebSocket Server/Client is running/connected
@@ -92,12 +92,12 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
9292 /**
9393 * Get the interval checking for lost connections
9494 * Default is 60 seconds
95- * @return the interval
95+ * @return the interval in seconds
9696 * @since 1.3.4
9797 */
9898 public int getConnectionLostTimeout () {
9999 synchronized (syncConnectionLost ) {
100- return connectionLostTimeout ;
100+ return ( int ) TimeUnit . NANOSECONDS . toSeconds ( connectionLostTimeout ) ;
101101 }
102102 }
103103
@@ -110,7 +110,7 @@ public int getConnectionLostTimeout() {
110110 */
111111 public void setConnectionLostTimeout ( int connectionLostTimeout ) {
112112 synchronized (syncConnectionLost ) {
113- this .connectionLostTimeout = connectionLostTimeout ;
113+ this .connectionLostTimeout = TimeUnit . SECONDS . toNanos ( connectionLostTimeout ) ;
114114 if (this .connectionLostTimeout <= 0 ) {
115115 log .trace ("Connection lost timer stopped" );
116116 cancelConnectionLostTimer ();
@@ -183,9 +183,9 @@ public void run() {
183183 connections .clear ();
184184 try {
185185 connections .addAll ( getConnections () );
186- long current = ( System .currentTimeMillis () - ( connectionLostTimeout * 1500 ) );
186+ long minimumPongTime = (long ) ( System .nanoTime () - ( connectionLostTimeout * 1.5 ) );
187187 for ( WebSocket conn : connections ) {
188- executeConnectionLostDetection (conn , current );
188+ executeConnectionLostDetection (conn , minimumPongTime );
189189 }
190190 } catch ( Exception e ) {
191191 //Ignore this exception
@@ -194,20 +194,20 @@ public void run() {
194194 }
195195 };
196196
197- connectionLostCheckerFuture = connectionLostCheckerService .scheduleAtFixedRate (connectionLostChecker , connectionLostTimeout , connectionLostTimeout , TimeUnit .SECONDS );
197+ connectionLostCheckerFuture = connectionLostCheckerService .scheduleAtFixedRate (connectionLostChecker , connectionLostTimeout , connectionLostTimeout , TimeUnit .NANOSECONDS );
198198 }
199199
200200 /**
201201 * Send a ping to the endpoint or close the connection since the other endpoint did not respond with a ping
202202 * @param webSocket the websocket instance
203- * @param current the current time in milliseconds
203+ * @param minimumPongTime the lowest/oldest allowable last pong time ( in nanoTime) before we consider the connection to be lost
204204 */
205- private void executeConnectionLostDetection (WebSocket webSocket , long current ) {
205+ private void executeConnectionLostDetection (WebSocket webSocket , long minimumPongTime ) {
206206 if (!(webSocket instanceof WebSocketImpl )) {
207207 return ;
208208 }
209209 WebSocketImpl webSocketImpl = (WebSocketImpl ) webSocket ;
210- if ( webSocketImpl .getLastPong () < current ) {
210+ if ( webSocketImpl .getLastPong () < minimumPongTime ) {
211211 log .trace ("Closing connection due to no pong received: {}" , webSocketImpl );
212212 webSocketImpl .closeConnection ( CloseFrame .ABNORMAL_CLOSE , "The connection was closed because the other endpoint did not respond with a pong in time. For more information check: https://github.com/TooTallNate/Java-WebSocket/wiki/Lost-connection-detection" );
213213 } else {
0 commit comments