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
importjava.nio.channels.ByteChannel;
66
importjava.nio.channels.spi.AbstractSelectableChannel;
77

8+
importorg.java_websocket.WebSocket.Role;
9+
810
publicclassSocketChannelIOHelper{
911

1012
publicstaticbooleanread( finalByteBufferbuf, WebSocketImplws, ByteChannelchannel ) throwsIOException{
@@ -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
returnc != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
6870
}
69-
70-
publicstaticvoidwriteBlocking( WebSocketImplws, ByteChannelchannel ) throwsInterruptedException , IOException{
71-
assert ( channelinstanceofAbstractSelectableChannel == true ? ( (AbstractSelectableChannel) channel ).isBlocking() : true );
72-
assert ( channelinstanceofWrappedByteChannel == true ? ( (WrappedByteChannel) channel ).isBlocking() : true );
73-
74-
ByteBufferbuf = 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
intreadBytes;
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 ( IOExceptione ){
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
publicvoidsetParseMode( Rolerole ){
221221
this.role = role;
222222
}
223+
224+
publicRolegetRole(){
225+
returnrole;
226+
}
223227

224228
}

0 commit comments

Comments
(0)