Skip to content

Commit 56148a3

Browse files
committed
added not throwing flush method to the client
1 parent 9fe1bc1 commit 56148a3

3 files changed

Lines changed: 40 additions & 41 deletions

File tree

example/ChatServer.java

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

98
import org.java_websocket.WebSocket;
109
import org.java_websocket.WebSocketServer;
@@ -18,7 +17,7 @@ public class ChatServer extends WebSocketServer {
1817
public ChatServer( int port ) throws UnknownHostException {
1918
super( new InetSocketAddress( InetAddress.getByName( "localhost" ), port ) );
2019
}
21-
20+
2221
public ChatServer( InetSocketAddress address ) {
2322
super( address );
2423
}
@@ -85,11 +84,8 @@ public void onError( WebSocket conn, Exception ex ) {
8584
* When socket related I/O errors occur.
8685
*/
8786
public void sendToAll( String text ) throws InterruptedException {
88-
Set<WebSocket> con = connections();
89-
synchronized ( con ) {
90-
for( WebSocket c : con ) {
91-
c.send( text );
92-
}
87+
for( WebSocket c : connections() ) {
88+
c.send( text );
9389
}
9490
}
9591
}

src/org/java_websocket/WebSocketClient.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
import java.io.IOException;
44
import java.net.InetSocketAddress;
55
import java.net.URI;
6-
import java.nio.channels.*;
6+
import java.nio.channels.ClosedByInterruptException;
7+
import java.nio.channels.NotYetConnectedException;
8+
import java.nio.channels.SelectionKey;
9+
import java.nio.channels.Selector;
10+
import java.nio.channels.SocketChannel;
11+
import java.nio.channels.UnresolvedAddressException;
712
import java.util.Iterator;
813
import java.util.Set;
914
import java.util.concurrent.locks.Lock;
@@ -244,6 +249,16 @@ private void finishConnect() throws IOException , InvalidHandshakeException , In
244249
sendHandshake();
245250
}
246251

252+
public void flush() {
253+
try {
254+
conn.flush();
255+
} catch ( IOException e ) {
256+
onError( e );
257+
conn.closeConnection( CloseFrame.ABNROMAL_CLOSE, true );
258+
return;
259+
}
260+
}
261+
247262
private void sendHandshake() throws IOException , InvalidHandshakeException , InterruptedException {
248263
String path;
249264
String part1 = uri.getPath();

src/org/java_websocket/WebSocketServer.java

Lines changed: 21 additions & 33 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;
1312
import java.util.Iterator;
1413
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 Set<WebSocket> connections = new HashSet<WebSocket>();
35+
private final CopyOnWriteArraySet<WebSocket> connections;
3636
/**
3737
* The port number that this WebSocket server should listen on. Default is
3838
* WebSocket.DEFAULT_PORT.
@@ -83,6 +83,7 @@ public WebSocketServer( InetSocketAddress address ) {
8383
* instance should comply to.
8484
*/
8585
public WebSocketServer( InetSocketAddress address , Draft draft ) {
86+
this.connections = new CopyOnWriteArraySet<WebSocket>();
8687
this.draft = draft;
8788
setAddress( address );
8889
}
@@ -108,10 +109,8 @@ public void start() {
108109
* When socket related I/O errors occur.
109110
*/
110111
public void stop() throws IOException {
111-
synchronized ( connections ) {
112-
for( WebSocket ws : connections ) {
113-
ws.close( CloseFrame.NORMAL );
114-
}
112+
for( WebSocket ws : connections ) {
113+
ws.close( CloseFrame.NORMAL );
115114
}
116115
thread.interrupt();
117116
this.server.close();
@@ -120,13 +119,11 @@ public void stop() throws IOException {
120119

121120
/**
122121
* Returns a WebSocket[] of currently connected clients.
123-
* Its iterators will be failfast and its not judicious
124-
* to modify it.
125122
*
126-
* @return The currently connected clients.
123+
* @return The currently connected clients in a WebSocket[].
127124
*/
128125
public Set<WebSocket> connections() {
129-
return this.connections;
126+
return Collections.unmodifiableSet( this.connections );
130127
}
131128

132129
/**
@@ -211,18 +208,16 @@ public void run() {
211208
key.channel().register( selector, SelectionKey.OP_READ, conn );
212209
}
213210
}
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-
}
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 );
226221
}
227222
}
228223
} catch ( IOException ex ) {
@@ -232,9 +227,6 @@ public void run() {
232227
if( conn != null ) {
233228
conn.close( CloseFrame.ABNROMAL_CLOSE );
234229
}
235-
} catch ( Throwable e ) {
236-
System.out.println( e );
237-
e.printStackTrace();
238230
}
239231
}
240232
}
@@ -268,19 +260,15 @@ public final void onWebsocketMessage( WebSocket conn, byte[] blob ) {
268260

269261
@Override
270262
public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
271-
synchronized ( connections ) {
272-
if( this.connections.add( conn ) ) {
273-
onOpen( conn, (ClientHandshake) handshake );
274-
}
263+
if( this.connections.add( conn ) ) {
264+
onOpen( conn, (ClientHandshake) handshake );
275265
}
276266
}
277267

278268
@Override
279269
public final void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote ) {
280-
synchronized ( connections ) {
281-
if( this.connections.remove( conn ) ) {
282-
onClose( conn, code, reason, remote );
283-
}
270+
if( this.connections.remove( conn ) ) {
271+
onClose( conn, code, reason, remote );
284272
}
285273
}
286274

0 commit comments

Comments
 (0)