Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch exceptions in AbstractWebSocket #722

Merged
merged 1 commit into from
Jun 18, 2018
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
52 changes: 31 additions & 21 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,18 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) {
if( WebSocketImpl.DEBUG )
System.out.println( "Connection lost timer restarted" );
//Reset all the pings
ArrayList<WebSocket> connections = new ArrayList<WebSocket>( getConnections() );
WebSocketImpl webSocketImpl;
for( WebSocket conn : connections ) {
if( conn instanceof WebSocketImpl ) {
webSocketImpl = ( WebSocketImpl ) conn;
webSocketImpl.updateLastPong();
try {
ArrayList<WebSocket> connections = new ArrayList<WebSocket>( getConnections() );
WebSocketImpl webSocketImpl;
for( WebSocket conn : connections ) {
if( conn instanceof WebSocketImpl ) {
webSocketImpl = ( WebSocketImpl ) conn;
webSocketImpl.updateLastPong();
}
}
} catch (Exception e) {
if (WebSocketImpl.DEBUG)
System.out.println("Exception during connection lost restart: " + e.getMessage());
}
restartConnectionLostTimer();
}
Expand Down Expand Up @@ -158,25 +163,30 @@ private void restartConnectionLostTimer() {
@Override
public void run() {
connections.clear();
connections.addAll( getConnections() );
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 , "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");
} else {
if (webSocketImpl.isOpen()) {
webSocketImpl.sendPing();
try {
connections.addAll( getConnections() );
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, "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" );
} else {
if (WebSocketImpl.DEBUG)
System.out.println("Trying to ping a non open connection: " + conn.toString());
if( webSocketImpl.isOpen() ) {
webSocketImpl.sendPing();
} else {
if( WebSocketImpl.DEBUG )
System.out.println( "Trying to ping a non open connection: " + conn.toString() );
}
}
}
}
} catch ( Exception e ) {
if (WebSocketImpl.DEBUG)
System.out.println("Exception during connection lost ping: " + e.getMessage());
}
connections.clear();
}
Expand Down