Skip to content

Commit 15f49ed

Browse files
committed
fixed websocket client socket not being closed in some cases and server
waiting for clients to close connection first
1 parent 6042525 commit 15f49ed

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.nio.channels.ByteChannel;
66
import java.nio.channels.spi.AbstractSelectableChannel;
77

8+
import org.java_websocket.WebSocket.Role;
9+
810
public class SocketChannelIOHelper {
911

1012
public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException {
@@ -59,21 +61,11 @@ public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws
5961
} while ( buffer != null );
6062
}
6163

62-
/*if( ws.outQueue.isEmpty() && ws.isFlushAndClose() ) {//
64+
if( ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft().getRole() == Role.SERVER ) {//
6365
synchronized ( ws ) {
6466
ws.closeConnection();
6567
}
66-
}*/
68+
}
6769
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
6870
}
69-
70-
public static void writeBlocking( WebSocketImpl ws, ByteChannel channel ) throws InterruptedException , IOException {
71-
assert ( channel instanceof AbstractSelectableChannel == true ? ( (AbstractSelectableChannel) channel ).isBlocking() : true );
72-
assert ( channel instanceof WrappedByteChannel == true ? ( (WrappedByteChannel) channel ).isBlocking() : true );
73-
74-
ByteBuffer buf = ws.outQueue.take();
75-
while ( buf.hasRemaining() )
76-
channel.write( buf );
77-
}
78-
7971
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void run() {
181181
int readBytes;
182182

183183
try {
184-
while ( ( readBytes = istream.read( rawbuffer ) ) != -1 ) {
184+
while ( !isClosed() && ( readBytes = istream.read( rawbuffer ) ) != -1 ) {
185185
engine.decode( ByteBuffer.wrap( rawbuffer, 0, readBytes ) );
186186
}
187187
engine.eot();
@@ -276,6 +276,12 @@ public final void onWebsocketClose( WebSocket conn, int code, String reason, boo
276276
closeLatch.countDown();
277277
if( writeThread != null )
278278
writeThread.interrupt();
279+
try {
280+
if( socket != null )
281+
socket.close();
282+
} catch ( IOException e ) {
283+
onWebsocketError( this, e );
284+
}
279285
onClose( code, reason, remote );
280286
}
281287

src/main/java/org/java_websocket/drafts/Draft.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,9 @@ public int checkAlloc( int bytecount ) throws LimitExedeedException , InvalidDat
220220
public void setParseMode( Role role ) {
221221
this.role = role;
222222
}
223+
224+
public Role getRole() {
225+
return role;
226+
}
223227

224228
}

0 commit comments

Comments
 (0)