Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.java_websocket.framing.CloseFrame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;
Expand Down Expand Up @@ -104,23 +105,30 @@ protected void startConnectionLostTimer() {
cancelConnectionLostTimer();
connectionLostTimer = new Timer();
connectionLostTimerTask = new TimerTask() {

/**
* Keep the connections in a separate list to not cause deadlocks
*/
private ArrayList<WebSocket> connections = new ArrayList<WebSocket>( );
@Override
public void run() {
Collection<WebSocket> con = connections();
synchronized ( con ) {
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
for( WebSocket conn : con ) {
if (conn instanceof WebSocketImpl) {
if( ((WebSocketImpl)conn).getLastPong() < current ) {
if (WebSocketImpl.DEBUG)
System.out.println("Closing connection due to no pong received: " + conn.toString());
conn.close( CloseFrame.ABNORMAL_CLOSE );
} else {
conn.sendPing();
}
}
}
}
connections.clear();
connections.addAll( connections() );
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
WebSocketImpl webSocketImpl;
for( WebSocket conn : connections ) {
if (conn instanceof WebSocketImpl) {
webSocketImpl = (WebSocketImpl)conn;
if( webSocketImpl.getLastPong() < current ) {
if (WebSocketImpl.DEBUG)
System.out.println("Closing connection due to no pong received: " + conn.toString());
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE , false );
} else {
webSocketImpl.sendPing();
}
}
}
connections.clear();
}
};
connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 );
Expand Down