Skip to content

Commit 1c655b4

Browse files
committed
Fixed situation in which a connection would not be closed by the server if the client connects via ws also the server expects wss
1 parent 71e6a8f commit 1c655b4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.nio.ByteBuffer;
99
import java.nio.channels.ByteChannel;
1010
import java.nio.channels.CancelledKeyException;
11+
import java.nio.channels.SelectableChannel;
1112
import java.nio.channels.SelectionKey;
1213
import java.nio.channels.Selector;
1314
import java.nio.channels.ServerSocketChannel;
@@ -370,7 +371,7 @@ public void run() {
370371
} catch ( IOException ex ) {
371372
if( key != null )
372373
key.cancel();
373-
handleIOException( conn, ex );
374+
handleIOException( key, conn, ex );
374375
} catch ( InterruptedException e ) {
375376
return;// FIXME controlled shutdown
376377
}
@@ -416,10 +417,21 @@ private void pushBuffer( ByteBuffer buf ) throws InterruptedException {
416417
buffers.put( buf );
417418
}
418419

419-
private void handleIOException( WebSocket conn, IOException ex ) {
420+
private void handleIOException( SelectionKey key, WebSocket conn, IOException ex ) {
420421
onWebsocketError( conn, ex );// conn may be null here
421422
if( conn != null ) {
422423
conn.closeConnection( CloseFrame.ABNORMAL_CLOSE, ex.getMessage() );
424+
} else if( key != null ) {
425+
SelectableChannel channel = key.channel();
426+
if( channel != null && channel.isOpen() ) { // this could be the case if the IOException ex is a SSLException
427+
try {
428+
channel.close();
429+
} catch ( IOException e ) {
430+
// there is nothing that must be done here
431+
}
432+
if( WebSocketImpl.DEBUG )
433+
System.out.println( "Connection closed because of" + ex );
434+
}
423435
}
424436
}
425437

0 commit comments

Comments
 (0)