Skip to content

Commit b13c890

Browse files
committed
Adding clear method getConnections
1 parent bd9022e commit b13c890

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void restartConnectionLostTimer() {
138138
@Override
139139
public void run() {
140140
connections.clear();
141-
connections.addAll( connections() );
141+
connections.addAll( getConnections() );
142142
long current = (System.currentTimeMillis()-(connectionLostTimeout * 1500));
143143
WebSocketImpl webSocketImpl;
144144
for( WebSocket conn : connections ) {
@@ -169,7 +169,7 @@ public void run() {
169169
* @return the currently available connections
170170
* @since 1.3.4
171171
*/
172-
protected abstract Collection<WebSocket> connections();
172+
protected abstract Collection<WebSocket> getConnections();
173173

174174
/**
175175
* Cancel any running timer for the connection lost detection

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ public <T> void setAttachment(T attachment) {
329329
}
330330

331331
@Override
332-
protected Collection<WebSocket> connections() {
332+
protected Collection<WebSocket> getConnections() {
333333
return Collections.singletonList((WebSocket ) engine );
334334
}
335335

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,29 @@ public void stop() throws IOException , InterruptedException {
264264
}
265265

266266
/**
267+
* PLEASE use the method getConnections() in the future!
268+
*
267269
* Returns a WebSocket[] of currently connected clients.
268270
* Its iterators will be failfast and its not judicious
269271
* to modify it.
270272
*
271273
* @return The currently connected clients.
274+
*
272275
*/
276+
@Deprecated
273277
public Collection<WebSocket> connections() {
274-
return this.connections;
278+
return getConnections();
279+
}
280+
281+
/**
282+
* Returns all currently connected clients.
283+
* This collection does not allow any modification e.g. removing a client.
284+
*
285+
* @return A unmodifiable collection of all currently connected clients
286+
* @since 1.3.8
287+
*/
288+
public Collection<WebSocket> getConnections() {
289+
return Collections.unmodifiableCollection( new ArrayList<WebSocket>(connections) );
275290
}
276291

277292
public InetSocketAddress getAddress() {

0 commit comments

Comments
 (0)