|
27 | 27 |
|
28 | 28 | import org.java_websocket.framing.CloseFrame; |
29 | 29 |
|
| 30 | +import java.util.ArrayList; |
30 | 31 | import java.util.Collection; |
31 | 32 | import java.util.Timer; |
32 | 33 | import java.util.TimerTask; |
@@ -104,23 +105,30 @@ protected void startConnectionLostTimer() { |
104 | 105 | cancelConnectionLostTimer(); |
105 | 106 | connectionLostTimer = new Timer(); |
106 | 107 | connectionLostTimerTask = new TimerTask() { |
| 108 | + |
| 109 | + /** |
| 110 | + * Keep the connections in a separate list to not cause deadlocks |
| 111 | + */ |
| 112 | + private ArrayList<WebSocket> connections = new ArrayList<WebSocket>( ); |
107 | 113 | @Override |
108 | 114 | public void run() { |
109 | | - Collection<WebSocket> con = connections(); |
110 | | - synchronized ( con ) { |
111 | | - long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500)); |
112 | | - for( WebSocket conn : con ) { |
113 | | - if (conn instanceof WebSocketImpl) { |
114 | | - if( ((WebSocketImpl)conn).getLastPong() < current ) { |
115 | | - if (WebSocketImpl.DEBUG) |
116 | | - System.out.println("Closing connection due to no pong received: " + conn.toString()); |
117 | | - conn.close( CloseFrame.ABNORMAL_CLOSE ); |
118 | | - } else { |
119 | | - conn.sendPing(); |
120 | | - } |
121 | | - } |
122 | | - } |
123 | | - } |
| 115 | + connections.clear(); |
| 116 | + connections.addAll( connections() ); |
| 117 | + long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500)); |
| 118 | + WebSocketImpl webSocketImpl; |
| 119 | + for( WebSocket conn : connections ) { |
| 120 | + if (conn instanceof WebSocketImpl) { |
| 121 | + webSocketImpl = (WebSocketImpl)conn; |
| 122 | + if( webSocketImpl.getLastPong() < current ) { |
| 123 | + if (WebSocketImpl.DEBUG) |
| 124 | + System.out.println("Closing connection due to no pong received: " + conn.toString()); |
| 125 | + webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE , false ); |
| 126 | + } else { |
| 127 | + webSocketImpl.sendPing(); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + connections.clear(); |
124 | 132 | } |
125 | 133 | }; |
126 | 134 | connectionLostTimer.scheduleAtFixedRate( connectionLostTimerTask,connectionLostTimeout * 1000, connectionLostTimeout * 1000 ); |
|
0 commit comments