Skip to content

Commit ca85bbf

Browse files
authored
Merge pull request TooTallNate#610 from marci4/master
Check if connection is open on sendPing & change readystate on closeConnection
2 parents 535df42 + eaf1b86 commit ca85bbf

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ bin
1616
/doc
1717
*.xml
1818
.idea/.name
19-
*.jks
19+
*.jks
20+
lib/junit-4.7.jar
21+
*.jar

Jenkinsfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
node {
2+
def mvnHome
3+
stage('Preparation') { // for display purposes
4+
// Get some code from a GitHub repository
5+
git 'https://github.com/marci4/Java-WebSocket-Dev.git'
6+
// Get the Maven tool.
7+
// ** NOTE: This 'M3' Maven tool must be configured
8+
// ** in the global configuration.
9+
mvnHome = tool 'M3'
10+
}
11+
stage('Build') {
12+
// Run the maven build
13+
if (isUnix()) {
14+
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
15+
} else {
16+
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
17+
}
18+
}
19+
stage('Test') {
20+
if (isUnix()) {
21+
sh "'${mvnHome}/bin/mvn' test package"
22+
} else {
23+
bat(/"${mvnHome}\bin\mvn" test package/)
24+
}
25+
}
26+
stage('Results') {
27+
junit '**/target/surefire-reports/TEST-*.xml'
28+
archive 'target/*.jar'
29+
}
30+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ public void run() {
139139
System.out.println("Closing connection due to no pong received: " + conn.toString());
140140
webSocketImpl.closeConnection( CloseFrame.ABNORMAL_CLOSE , false );
141141
} else {
142-
webSocketImpl.sendPing();
142+
if (webSocketImpl.isOpen()) {
143+
webSocketImpl.sendPing();
144+
} else {
145+
if (WebSocketImpl.DEBUG)
146+
System.out.println("Trying to ping a non open connection: " + conn.toString());
147+
}
143148
}
144149
}
145150
}

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)