Skip to content

Commit 8ad0ba4

Browse files
committed
Change readystate on closeConnection
1 parent 6fecfb1 commit 8ad0ba4

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎src/main/java/org/java_websocket/AbstractWebSocket.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ public void run() {
137137
if( webSocketImpl.getLastPong() < current ) {
138138
if (WebSocketImpl.DEBUG)
139139
System.out.println("Closing connection due to no pong received: " + conn.toString());
140-
webSocketImpl.close( CloseFrame.ABNORMAL_CLOSE , "Closing connection due to no pong received", false );
141140
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE , false );
142141
} else {
143142
if (webSocketImpl.isOpen()) {

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public void decode( ByteBuffer socketBuffer ) {
201201
if( DEBUG )
202202
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + '}' );
203203

204-
if( getReadyState() != READYSTATE.NOT_YET_CONNECTED ) {
205-
if( getReadyState() == READYSTATE.OPEN ) {
204+
if( getReadyState() != READYSTATE.NOT_YET_CONNECTED ) {
205+
if( getReadyState() == READYSTATE.OPEN ) {
206206
decodeFrames( socketBuffer );
207207
}
208208
} else {
@@ -406,12 +406,12 @@ private ByteBuffer generateHttpResponseDueToError( int errorCode ) {
406406
return ByteBuffer.wrap( Charsetfunctions.asciiBytes( "HTTP/1.1 " + errorCodeDescription + "\r\nContent-Type: text/html\nServer: TooTallNate Java-WebSocket\r\nContent-Length: " + ( 48 + errorCodeDescription.length() ) + "\r\n\r\n<html><head></head><body><h1>" + errorCodeDescription + "</h1></body></html>" ) );
407407
}
408408

409-
public void close( int code, String message, boolean remote ) {
410-
if( getReadyState() != READYSTATE.CLOSING && readystate != READYSTATE.CLOSED ) {
411-
if( getReadyState() == READYSTATE.OPEN ) {
409+
public synchronized void close( int code, String message, boolean remote ) {
410+
if( getReadyState() != READYSTATE.CLOSING && readystate != READYSTATE.CLOSED ) {
411+
if( getReadyState() == READYSTATE.OPEN ) {
412412
if( code == CloseFrame.ABNORMAL_CLOSE ) {
413413
assert ( !remote );
414-
setReadyState(READYSTATE.CLOSING);
414+
setReadyState( READYSTATE.CLOSING );
415415
flushAndClose( code, message, false );
416416
return;
417417
}
@@ -424,7 +424,7 @@ public void close( int code, String message, boolean remote ) {
424424
wsl.onWebsocketError( this, e );
425425
}
426426
}
427-
if (isOpen()) {
427+
if( isOpen() ) {
428428
CloseFrame closeFrame = new CloseFrame();
429429
closeFrame.setReason( message );
430430
closeFrame.setCode( code );
@@ -445,7 +445,7 @@ public void close( int code, String message, boolean remote ) {
445445
} else {
446446
flushAndClose( CloseFrame.NEVER_CONNECTED, message, false );
447447
}
448-
setReadyState(READYSTATE.CLOSING);
448+
setReadyState( READYSTATE.CLOSING );
449449
tmpHandshakeBytes = null;
450450
return;
451451
}
@@ -471,7 +471,12 @@ public synchronized void closeConnection( int code, String message, boolean remo
471471
if( getReadyState() == READYSTATE.CLOSED ) {
472472
return;
473473
}
474-
474+
//Methods like eot() call this method without calling onClose(). Due to that reason we have to adjust the readystate manually
475+
if( getReadyState() == READYSTATE.OPEN ) {
476+
if( code == CloseFrame.ABNORMAL_CLOSE ) {
477+
setReadyState( READYSTATE.CLOSING );
478+
}
479+
}
475480
if( key != null ) {
476481
// key.attach( null ); //see issue #114
477482
key.cancel();
@@ -497,8 +502,7 @@ public synchronized void closeConnection( int code, String message, boolean remo
497502
if( draft != null )
498503
draft.reset();
499504
handshakerequest = null;
500-
501-
setReadyState(READYSTATE.CLOSED);
505+
setReadyState( READYSTATE.CLOSED );
502506
}
503507

504508
protected void closeConnection( int code, boolean remote ) {
@@ -597,7 +601,7 @@ private void send( Collection<Framedata> frames ) {
597601
if( !isOpen() ) {
598602
throw new WebsocketNotConnectedException();
599603
}
600-
if( frames == null) {
604+
if( frames == null ) {
601605
throw new IllegalArgumentException();
602606
}
603607
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<ByteBuffer>();
@@ -691,7 +695,7 @@ private void write( List<ByteBuffer> bufs ) {
691695
private void open( Handshakedata d ) {
692696
if( DEBUG )
693697
System.out.println( "open using draft: " + draft );
694-
setReadyState(READYSTATE.OPEN);
698+
setReadyState( READYSTATE.OPEN );
695699
try {
696700
wsl.onWebsocketOpen( this, d );
697701
} catch ( RuntimeException e ) {

0 commit comments

Comments
 (0)