Skip to content

Commit a878054

Browse files
committed
additional change for 9418783
improved websocket closing behavior
1 parent 06cb47b commit a878054

1 file changed

Lines changed: 8 additions & 14 deletions

File tree

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.channels.Selector;
1010
import java.nio.channels.SocketChannel;
1111
import java.nio.channels.UnresolvedAddressException;
12+
import java.nio.charset.CharacterCodingException;
1213
import java.util.Iterator;
1314
import java.util.Set;
1415

@@ -96,17 +97,10 @@ public void connect() {
9697
thread.start();
9798
}
9899

99-
/**
100-
* Calls <var>close</var> on the underlying SocketChannel, which in turn
101-
* closes the socket connection, and ends the client socket thread.
102-
*
103-
*/
104-
public Thread close() {
105-
if( thread == null ) {
106-
throw new IllegalStateException( "Socket has not yet been started or is already closed" );
100+
public void close() {
101+
if( thread != null ) {
102+
thread.interrupt();
107103
}
108-
thread.interrupt();
109-
return thread;
110104
}
111105

112106
/**
@@ -197,7 +191,7 @@ protected void interruptableRun() {
197191
} catch ( RuntimeException e ) {
198192
// this catch case covers internal errors only and indicates a bug in this websocket implementation
199193
onError( e );
200-
conn.closeConnection( CloseFrame.BUGGYCLOSE, e.toString() );
194+
conn.closeConnection( CloseFrame.BUGGYCLOSE, e.toString(), false );
201195
return;
202196
}
203197
conn.close( CloseFrame.NORMAL ); // close() is synchronously calling onClose(conn) so we don't have to
@@ -277,9 +271,9 @@ public void onOpen( WebSocket conn ) {
277271
* @param conn
278272
*/
279273
@Override
280-
public void onClose( WebSocket conn ) {
274+
public void onClose( WebSocket conn, int code, String reason ) {
281275
thread.interrupt();
282-
onClose();
276+
onClose( code, reason );
283277
}
284278

285279
/**
@@ -300,6 +294,6 @@ public void onWriteDemand( WebSocket conn ) {
300294
// ABTRACT METHODS /////////////////////////////////////////////////////////
301295
public abstract void onMessage( String message );
302296
public abstract void onOpen();
303-
public abstract void onClose();
297+
public abstract void onClose( int code, String reason );
304298
public abstract void onError( Exception ex );
305299
}

0 commit comments

Comments
 (0)