Skip to content

Commit 06cb47b

Browse files
committed
improved websocket closing behavior
1 parent d04c11d commit 06cb47b

7 files changed

Lines changed: 34 additions & 14 deletions

File tree

‎example/AutobahnClientTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public static void main( String[] args ) {
8080
uri = URI.create( serverlocation + "/runCase?case=" + spl[ 1 ] + "&agent=" + clientname );
8181

8282
} else if( line.startsWith( "u" ) ) {
83+
WebSocket.DEBUG = false;
8384
uri = URI.create( serverlocation + "/updateReports?agent=" + clientname );
8485
} else if( line.startsWith( "d" ) ) {
8586
try {
@@ -152,7 +153,8 @@ public void onOpen() {
152153
}
153154

154155
@Override
155-
public void onClose() {
156+
public void onClose( int code, String reason ) {
157+
System.out.println( "Closed: " + code + " " + reason );
156158
}
157159

158160
}

‎example/ChatClient.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.awt.event.ActionEvent;
44
import java.awt.event.ActionListener;
55
import java.awt.event.WindowEvent;
6-
import java.io.IOException;
76
import java.net.URI;
87
import java.net.URISyntaxException;
98

@@ -114,8 +113,8 @@ public void onOpen() {
114113
ta.setCaretPosition( ta.getDocument().getLength() );
115114
}
116115

117-
public void onClose() {
118-
ta.append( "You have been disconnected from: " + getURI() + "\n" );
116+
public void onClose( int code, String reason ) {
117+
ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
119118
ta.setCaretPosition( ta.getDocument().getLength() );
120119
connect.setEnabled( true );
121120
uriField.setEditable( true );

‎src/net/tootallnate/websocket/CloseFrameBuilder.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,11 @@ public String toString() {
7878
return super.toString() + "code: " + getCloseCode();
7979
}
8080

81+
@Override
82+
public byte[] getPayloadData() {
83+
if(getCloseCode()==NOCODE)
84+
return new byte[ 0 ];
85+
return super.getPayloadData();
86+
}
87+
8188
}

‎src/net/tootallnate/websocket/FramedataImpl1.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void append( Framedata nextframe ) throws InvalidFrameException {
8080

8181
@Override
8282
public String toString() {
83-
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", masked:" + getTransfereMasked() + ", payloadlength:" + unmaskedpayload.limit() + ", payload:" + Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) + "}";
83+
return "Framedata{ optcode:" + getOpcode() + ", fin:" + isFin() + ", payloadlength:" + unmaskedpayload.limit() + ", payload:" + Charsetfunctions.utf8Bytes( new String( unmaskedpayload.array() ) ) + "}";
8484
}
8585

8686
}

‎src/net/tootallnate/websocket/WebSocket.java‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ else if( curop == Opcode.CLOSING ) {
249249
}
250250
if( closeHandshakeSent ) {
251251
// complete the close handshake by disconnecting
252-
closeConnection( code, reason );
252+
closeConnection( code, reason, true );
253253
} else {
254254
// echo close handshake
255255
close( code, reason );
256-
closeConnection( code, reason );
256+
closeConnection( code, reason, false );
257257
}
258258
continue;
259259
} else if( curop == Opcode.PING ) {
@@ -303,25 +303,35 @@ public void close( int code, String message ) {
303303
try {
304304
closeDirect( code, message );
305305
} catch ( IOException e ) {
306-
closeConnection( CloseFrame.ABNROMAL_CLOSE );
306+
closeConnection( CloseFrame.ABNROMAL_CLOSE,true );
307307
}
308308
}
309309

310310
public void closeDirect( int code, String message ) throws IOException {
311311
if( !closeHandshakeSent ) {
312+
if( handshakeComplete ) {
313+
flush();
314+
sendFrameDirect( new CloseFrameBuilder( code, message ) );
315+
}
316+
if( code == CloseFrame.PROTOCOL_ERROR )// this endpoint found a PROTOCOL_ERROR
317+
closeConnection( code, false );
312318
closeHandshakeSent = true;
313-
flush();
314-
sendFrameDirect( new CloseFrameBuilder( code, message ) );
315319
return;
316320
}
317321
}
318322

319323
/**
320324
* closes the socket no matter if the closing handshake completed.
321325
* Does not send any not yet written data before closing.
322-
* Calling this method twice will have no effect.
326+
* Calling this method more than once will have no effect.
327+
*
328+
* @param remote
329+
* Indicates who "generated" <code>code</code>.<br>
330+
* <code>true</code> means that this endpoint received the <code>code</code> from the other endpoint.<br>
331+
* false means this endpoint decided to send the given code,<br>
332+
* <code>remote</code> may also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the <code>code</code> but close the connection the same time this endpoint does do but with an other <code>code</code>. <br>
323333
**/
324-
public void closeConnection( int code, String message ) {
334+
public void closeConnection( int code, String message, boolean remote ) {
325335
if( connectionClosed ) {
326336
return;
327337
}
@@ -338,8 +348,8 @@ public void closeConnection( int code, String message ) {
338348
handshakerequest = null;
339349
}
340350

341-
public void closeConnection( int code ) {
342-
closeConnection( code, "" );
351+
public void closeConnection( int code, boolean remote ) {
352+
closeConnection( code, "", remote );
343353
}
344354

345355
public void close( int code ) {

‎src/net/tootallnate/websocket/WebSocketAdapter.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ public String getFlashPolicy( WebSocket conn ) {
6868
@Override
6969
public void onError( WebSocket conn, Exception ex ) {
7070
}
71+
7172
}

‎src/net/tootallnate/websocket/exeptions/InvalidDataException.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public InvalidDataException( int closecode ) {
88

99
public InvalidDataException( int closecode , String s ) {
1010
super( s );
11+
this.closecode = closecode;
1112
}
1213

1314
public InvalidDataException( int closecode , Throwable t ) {

0 commit comments

Comments
 (0)