Skip to content

Commit 2ef1559

Browse files
committed
fixed suppressed close frame on the client side & minor changes
1 parent d243815 commit 2ef1559

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/main/java/org/java_websocket/SocketChannelIOHelper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws
3939
WrappedByteChannel c = (WrappedByteChannel) sockchannel;
4040
if( c.isNeedWrite() ) {
4141
c.writeMore();
42-
return !c.isNeedWrite();
4342
}
44-
return true;
4543
}
4644
} else {
4745
do {// FIXME writing as much as possible is unfair!!

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,17 @@ protected synchronized void closeConnection( int code, String message, boolean r
398398
}
399399
connectionClosed = true;
400400

401-
if( key != null ) {
401+
/*if( key != null ) {
402402
// key.attach( null ); //see issue #114
403403
key.cancel();
404404
try {
405405
channel.close();
406406
} catch ( IOException e ) {
407407
wsl.onWebsocketError( this, e );
408408
}
409-
}
410-
// sockchannel.close();
409+
}*/
410+
wsl.onWriteDemand( this ); // ensures that all outgoing frames are flushed before closing the connection
411+
411412
this.wsl.onWebsocketClose( this, code, message, remote );
412413
if( draft != null )
413414
draft.reset();

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
5858
private SocketChannel channel = null;
5959

6060
private ByteChannel wrappedchannel = null;
61+
62+
private SelectionKey key = null;
6163
/**
6264
* The 'Selector' used to get event keys from the underlying socket.
6365
*/
@@ -180,7 +182,7 @@ private void tryToConnect( InetSocketAddress remote ) throws IOException {
180182
channel.configureBlocking( false );
181183
channel.connect( remote );
182184
selector = Selector.open();
183-
channel.register( selector, SelectionKey.OP_CONNECT );
185+
key = channel.register( selector, SelectionKey.OP_CONNECT );
184186
}
185187

186188
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
@@ -369,7 +371,6 @@ public final void onWebsocketOpen( WebSocket conn, Handshakedata handshake ) {
369371
*/
370372
@Override
371373
public final void onWebsocketClose( WebSocket conn, int code, String reason, boolean remote ) {
372-
thread.interrupt();
373374
onClose( code, reason, remote );
374375
}
375376

@@ -385,7 +386,7 @@ public final void onWebsocketError( WebSocket conn, Exception ex ) {
385386

386387
@Override
387388
public final void onWriteDemand( WebSocket conn ) {
388-
channel.keyFor( selector ).interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
389+
key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
389390
selector.wakeup();
390391
}
391392

0 commit comments

Comments
 (0)