Skip to content

Commit 44dee6c

Browse files
committed
onError won't be called with IOExceptions any more! In the past a user were bothered with IOExceptions even though the connections were closed properly which is confusing! Whether a connection was closed cleanly is be reported by onClose. Additionally i changed WebSocketImpl::eot and reformatted code
1 parent 6b55fb9 commit 44dee6c

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/main/java/org/java_websocket/SocketChannelIOHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel
1212
buf.flip();
1313

1414
if( read == -1 ) {
15-
ws.eot( null );
15+
ws.eot();
1616
return false;
1717
}
1818
return read != 0;
@@ -24,7 +24,7 @@ public static boolean readMore( final ByteBuffer buf, WebSocketImpl ws, WrappedB
2424
buf.flip();
2525

2626
if( read == -1 ) {
27-
ws.eot( null );
27+
ws.eot();
2828
return false;
2929
}
3030
return channel.isNeedRead();

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class WebSocketImpl extends WebSocket {
6868
**/
6969
public volatile WebSocketWorker workerThread; // TODO reset worker?
7070

71-
7271
/**
7372
* Determines whether to receive data as part of the
7473
* handshake, or as part of text/data frame transmitted over the websocket.
@@ -83,7 +82,6 @@ public class WebSocketImpl extends WebSocket {
8382
*/
8483
private volatile boolean connectionClosed = false;
8584

86-
8785
/**
8886
* The listener to notify of WebSocket events.
8987
*/
@@ -421,22 +419,18 @@ protected void closeConnection( int code, boolean remote ) {
421419
closeConnection( code, "", remote );
422420
}
423421

424-
public void eot( Exception e ) {
425-
if( e == null || e instanceof IOException ) {
426-
if( draft == null ) {
422+
public void eot() {
423+
if( draft == null ) {
424+
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
425+
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.NONE ) {
426+
closeConnection( CloseFrame.NORMAL, true );
427+
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.ONEWAY ) {
428+
if( role == Role.SERVER )
427429
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
428-
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.NONE ) {
430+
else
429431
closeConnection( CloseFrame.NORMAL, true );
430-
} else if( draft.getCloseHandshakeType() == CloseHandshakeType.ONEWAY ) {
431-
if( role == Role.SERVER )
432-
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
433-
else
434-
closeConnection( CloseFrame.NORMAL, true );
435-
} else {
436-
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
437-
}
438432
} else {
439-
closeConnection( CloseFrame.BUGGYCLOSE, e.toString(), false );
433+
closeConnection( CloseFrame.ABNORMAL_CLOSE, true );
440434
}
441435
}
442436

@@ -542,7 +536,7 @@ public void startHandshake( ClientHandshakeBuilder handshakedata ) throws Invali
542536
private void write( ByteBuffer buf ) {
543537
if( DEBUG )
544538
System.out.println( "write(" + buf.remaining() + "): {" + ( buf.remaining() > 1000 ? "too big to display" : new String( buf.array() ) ) + "}" );
545-
539+
546540
outQueue.add( buf );
547541
/*try {
548542
outQueue.put( buf );

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void run() {
188188
if( thread == null )
189189
thread = Thread.currentThread();
190190
interruptableRun();
191-
191+
192192
assert ( !channel.isOpen() );
193193

194194
try {
@@ -228,6 +228,7 @@ private final void interruptableRun() {
228228
key = i.next();
229229
i.remove();
230230
if( !key.isValid() ) {
231+
conn.eot();
231232
continue;
232233
}
233234
if( key.isReadable() && SocketChannelIOHelper.read( buff, this.conn, wrappedchannel ) ) {
@@ -260,13 +261,14 @@ private final void interruptableRun() {
260261
}
261262

262263
} catch ( CancelledKeyException e ) {
264+
conn.eot();
263265
} catch ( IOException e ) {
264-
onError( e );
265-
conn.close( CloseFrame.ABNORMAL_CLOSE );
266+
// onError( e );
267+
conn.eot();
266268
} catch ( RuntimeException e ) {
267269
// this catch case covers internal errors only and indicates a bug in this websocket implementation
268270
onError( e );
269-
conn.eot( e );
271+
conn.close( CloseFrame.ABNORMAL_CLOSE );
270272
}
271273
}
272274

@@ -276,11 +278,9 @@ private int getPort() {
276278
String scheme = uri.getScheme();
277279
if( scheme.equals( "wss" ) ) {
278280
return WebSocket.DEFAULT_WSS_PORT;
279-
}
280-
else if( scheme.equals( "ws" ) ) {
281+
} else if( scheme.equals( "ws" ) ) {
281282
return WebSocket.DEFAULT_PORT;
282-
}
283-
else{
283+
} else {
284284
throw new RuntimeException( "unkonow scheme" + scheme );
285285
}
286286
}
@@ -413,5 +413,3 @@ public interface WebSocketClientFactory extends WebSocketFactory {
413413
public ByteChannel wrapChannel( SelectionKey key, String host, int port ) throws IOException;
414414
}
415415
}
416-
417-

0 commit comments

Comments
 (0)