Skip to content

Commit 9fe1bc1

Browse files
committed
Changed the type of the server internal collection of connected clients.
1 parent f0ef4df commit 9fe1bc1

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

‎example/ChatServer.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.InetAddress;
55
import java.net.InetSocketAddress;
66
import java.net.UnknownHostException;
7+
import java.util.Set;
78

89
import org.java_websocket.WebSocket;
910
import org.java_websocket.WebSocketServer;
@@ -17,7 +18,7 @@ public class ChatServer extends WebSocketServer {
1718
public ChatServer( int port ) throws UnknownHostException {
1819
super( new InetSocketAddress( InetAddress.getByName( "localhost" ), port ) );
1920
}
20-
21+
2122
public ChatServer( InetSocketAddress address ) {
2223
super( address );
2324
}
@@ -84,8 +85,11 @@ public void onError( WebSocket conn, Exception ex ) {
8485
* When socket related I/O errors occur.
8586
*/
8687
public void sendToAll( String text ) throws InterruptedException {
87-
for( WebSocket c : connections() ) {
88-
c.send( text );
88+
Set<WebSocket> con = connections();
89+
synchronized ( con ) {
90+
for( WebSocket c : con ) {
91+
c.send( text );
92+
}
8993
}
9094
}
9195
}

‎src/org/java_websocket/WebSocketServer.java‎

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import java.nio.channels.ServerSocketChannel;
1010
import java.nio.channels.SocketChannel;
1111
import java.util.Collections;
12+
import java.util.HashSet;
1213
import java.util.Iterator;
1314
import java.util.Set;
14-
import java.util.concurrent.CopyOnWriteArraySet;
1515

1616
import org.java_websocket.drafts.Draft;
1717
import org.java_websocket.framing.CloseFrame;
@@ -32,7 +32,7 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
3232
* Holds the list of active WebSocket connections. "Active" means WebSocket
3333
* handshake is complete and socket can be written to, or read from.
3434
*/
35-
private final CopyOnWriteArraySet<WebSocket> connections;
35+
private final Set<WebSocket> connections = new HashSet<WebSocket>();
3636
/**
3737
* The port number that this WebSocket server should listen on. Default is
3838
* WebSocket.DEFAULT_PORT.
@@ -83,7 +83,6 @@ public WebSocketServer( InetSocketAddress address ) {
8383
* instance should comply to.
8484
*/
8585
public WebSocketServer( InetSocketAddress address , Draft draft ) {
86-
this.connections = new CopyOnWriteArraySet<WebSocket>();
8786
this.draft = draft;
8887
setAddress( address );
8988
}
@@ -109,8 +108,10 @@ public void start() {
109108
* When socket related I/O errors occur.
110109
*/
111110
public void stop() throws IOException {
112-
for( WebSocket ws : connections ) {
113-
ws.close( CloseFrame.NORMAL );
111+
synchronized ( connections ) {
112+
for( WebSocket ws : connections ) {
113+
ws.close( CloseFrame.NORMAL );
114+
}
114115
}
115116
thread.interrupt();
116117
this.server.close();
@@ -119,11 +120,13 @@ public void stop() throws IOException {
119120

120121
/**
121122
* Returns a WebSocket[] of currently connected clients.
123+
* Its iterators will be failfast and its not judicious
124+
* to modify it.
122125
*
123-
* @return The currently connected clients in a WebSocket[].
126+
* @return The currently connected clients.
124127
*/
125128
public Set<WebSocket> connections() {
126-
return Collections.unmodifiableSet( this.connections );
129+
return this.connections;
127130
}
128131

129132
/**
@@ -208,16 +211,18 @@ public void run() {
208211
key.channel().register( selector, SelectionKey.OP_READ, conn );
209212
}
210213
}
211-
Iterator<WebSocket> it = this.connections.iterator();
212-
while ( it.hasNext() ) {
213-
// We have to do this check here, and not in the thread that
214-
// adds the buffered data to the WebSocket, because the
215-
// Selector is not thread-safe, and can only be accessed
216-
// by this thread.
217-
conn = it.next();
218-
if( conn.hasBufferedData() ) {
219-
conn.flush();
220-
// key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
214+
synchronized ( connections ) {
215+
Iterator<WebSocket> it = this.connections.iterator();
216+
while ( it.hasNext() ) {
217+
// We have to do this check here, and not in the thread that
218+
// adds the buffered data to the WebSocket, because the
219+
// Selector is not thread-safe, and can only be accessed
220+
// by this thread.
221+
conn = it.next();
222+
if( conn.hasBufferedData() ) {
223+
conn.flush();
224+
// key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
225+
}
221226
}
222227
}
223228
} catch ( IOException ex ) {
@@ -227,6 +232,9 @@ public void run() {
227232
if( conn != null ) {
228233
conn.close( CloseFrame.ABNROMAL_CLOSE );
229234
}
235+
} catch ( Throwable e ) {
236+
System.out.println( e );
237+
e.printStackTrace();
230238
}
231239
}
232240
}
@@ -260,15 +268,19 @@ public final void onWebsocketMessage( WebSocket conn, byte[] blob ) {
260268

261269
@Override
262270
public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
263-
if( this.connections.add( conn ) ) {
264-
onOpen( conn, (ClientHandshake) handshake );
271+
synchronized ( connections ) {
272+
if( this.connections.add( conn ) ) {
273+
onOpen( conn, (ClientHandshake) handshake );
274+
}
265275
}
266276
}
267277

268278
@Override
269279
public final void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote ) {
270-
if( this.connections.remove( conn ) ) {
271-
onClose( conn, code, reason, remote );
280+
synchronized ( connections ) {
281+
if( this.connections.remove( conn ) ) {
282+
onClose( conn, code, reason, remote );
283+
}
272284
}
273285
}
274286

0 commit comments

Comments
 (0)