Skip to content

Commit c0ad64b

Browse files
authored
Merge pull request TooTallNate#722 from marci4/master
Catch exceptions in AbstractWebSocket
2 parents 41cf972 + 4c87342 commit c0ad64b

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,18 @@ public void setConnectionLostTimeout( int connectionLostTimeout ) {
102102
if( WebSocketImpl.DEBUG )
103103
System.out.println( "Connection lost timer restarted" );
104104
//Reset all the pings
105-
ArrayList<WebSocket> connections = new ArrayList<WebSocket>( getConnections() );
106-
WebSocketImpl webSocketImpl;
107-
for( WebSocket conn : connections ) {
108-
if( conn instanceof WebSocketImpl ) {
109-
webSocketImpl = ( WebSocketImpl ) conn;
110-
webSocketImpl.updateLastPong();
105+
try {
106+
ArrayList<WebSocket> connections = new ArrayList<WebSocket>( getConnections() );
107+
WebSocketImpl webSocketImpl;
108+
for( WebSocket conn : connections ) {
109+
if( conn instanceof WebSocketImpl ) {
110+
webSocketImpl = ( WebSocketImpl ) conn;
111+
webSocketImpl.updateLastPong();
112+
}
111113
}
114+
} catch (Exception e) {
115+
if (WebSocketImpl.DEBUG)
116+
System.out.println("Exception during connection lost restart: " + e.getMessage());
112117
}
113118
restartConnectionLostTimer();
114119
}
@@ -158,25 +163,30 @@ private void restartConnectionLostTimer() {
158163
@Override
159164
public void run() {
160165
connections.clear();
161-
connections.addAll( getConnections() );
162-
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
163-
WebSocketImpl webSocketImpl;
164-
for( WebSocket conn : connections ) {
165-
if (conn instanceof WebSocketImpl) {
166-
webSocketImpl = (WebSocketImpl)conn;
167-
if( webSocketImpl.getLastPong() < current ) {
168-
if (WebSocketImpl.DEBUG)
169-
System.out.println("Closing connection due to no pong received: " + conn.toString());
170-
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");
171-
} else {
172-
if (webSocketImpl.isOpen()) {
173-
webSocketImpl.sendPing();
166+
try {
167+
connections.addAll( getConnections() );
168+
long current = ( System.currentTimeMillis() - ( connectionLostTimeout * 1500 ) );
169+
WebSocketImpl webSocketImpl;
170+
for( WebSocket conn : connections ) {
171+
if( conn instanceof WebSocketImpl ) {
172+
webSocketImpl = ( WebSocketImpl ) conn;
173+
if( webSocketImpl.getLastPong() < current ) {
174+
if( WebSocketImpl.DEBUG )
175+
System.out.println( "Closing connection due to no pong received: " + conn.toString() );
176+
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" );
174177
} else {
175-
if (WebSocketImpl.DEBUG)
176-
System.out.println("Trying to ping a non open connection: " + conn.toString());
178+
if( webSocketImpl.isOpen() ) {
179+
webSocketImpl.sendPing();
180+
} else {
181+
if( WebSocketImpl.DEBUG )
182+
System.out.println( "Trying to ping a non open connection: " + conn.toString() );
183+
}
177184
}
178185
}
179186
}
187+
} catch ( Exception e ) {
188+
if (WebSocketImpl.DEBUG)
189+
System.out.println("Exception during connection lost ping: " + e.getMessage());
180190
}
181191
connections.clear();
182192
}

0 commit comments

Comments
 (0)