Skip to content

Commit 1b2e3db

Browse files
authored
Merge pull request TooTallNate#586 from marci4/master
Connection lost timer should not start on setter call
2 parents 7b4cdff + 7c34388 commit 1b2e3db

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) {
8181
this.connectionLostTimeout = connectionLostTimeout;
8282
if (this.connectionLostTimeout <= 0) {
8383
stopConnectionLostTimer();
84-
} else {
85-
startConnectionLostTimer();
8684
}
85+
if (connectionLostTimer != null || connectionLostTimerTask != null) {
86+
if( WebSocketImpl.DEBUG )
87+
System.out.println( "Connection lost timer restarted" );
88+
restartConnectionLostTimer();
89+
}
8790
}
8891

8992
/**
@@ -107,18 +110,25 @@ protected void startConnectionLostTimer() {
107110
}
108111
if (WebSocketImpl.DEBUG)
109112
System.out.println("Connection lost timer started");
110-
cancelConnectionLostTimer();
111-
connectionLostTimer = new Timer();
112-
connectionLostTimerTask = new TimerTask() {
113+
restartConnectionLostTimer();
114+
}
115+
116+
/**
117+
* This methods allows the reset of the connection lost timer in case of a changed parameter
118+
*/
119+
private void restartConnectionLostTimer() {
120+
cancelConnectionLostTimer();
121+
connectionLostTimer = new Timer();
122+
connectionLostTimerTask = new TimerTask() {
113123

114124
/**
115125
* Keep the connections in a separate list to not cause deadlocks
116126
*/
117127
private ArrayList<WebSocket> connections = new ArrayList<WebSocket>( );
118-
@Override
119-
public void run() {
120-
connections.clear();
121-
connections.addAll( connections() );
128+
@Override
129+
public void run() {
130+
connections.clear();
131+
connections.addAll( connections() );
122132
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
123133
WebSocketImpl webSocketImpl;
124134
for( WebSocket conn : connections ) {
@@ -134,12 +144,12 @@ public void run() {
134144
}
135145
}
136146
connections.clear();
137-
}
138-
};
139-
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
140-
}
147+
}
148+
};
149+
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
150+
}
141151

142-
/**
152+
/**
143153
* Getter to get all the currently available connections
144154
* @return the currently available connections
145155
*/

0 commit comments

Comments
 (0)