99import java .nio .channels .ServerSocketChannel ;
1010import java .nio .channels .SocketChannel ;
1111import java .util .Collections ;
12+ import java .util .HashSet ;
1213import java .util .Iterator ;
1314import java .util .Set ;
14- import java .util .concurrent .CopyOnWriteArraySet ;
1515
1616import org .java_websocket .drafts .Draft ;
1717import 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