From 4c873420b04c7b6ba5695f8708a768c0068e7a20 Mon Sep 17 00:00:00 2001 From: marci4 Date: Mon, 18 Jun 2018 19:13:57 +0200 Subject: [PATCH] Catch exceptions in AbstractWebSocket --- .../org/java_websocket/AbstractWebSocket.java | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/java_websocket/AbstractWebSocket.java b/src/main/java/org/java_websocket/AbstractWebSocket.java index 729ae925..fce27a11 100644 --- a/src/main/java/org/java_websocket/AbstractWebSocket.java +++ b/src/main/java/org/java_websocket/AbstractWebSocket.java @@ -102,13 +102,18 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) { if( WebSocketImpl.DEBUG ) System.out.println( "Connection lost timer restarted" ); //Reset all the pings - ArrayList connections = new ArrayList( getConnections() ); - WebSocketImpl webSocketImpl; - for( WebSocket conn : connections ) { - if( conn instanceof WebSocketImpl ) { - webSocketImpl = ( WebSocketImpl ) conn; - webSocketImpl.updateLastPong(); + try { + ArrayList connections = new ArrayList( 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(); } @@ -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(); }