2424import org .java_websocket .exceptions .InvalidDataException ;
2525import org .java_websocket .exceptions .InvalidFrameException ;
2626import org .java_websocket .exceptions .InvalidHandshakeException ;
27+ import org .java_websocket .exceptions .WebsocketNotConnectedException ;
2728import org .java_websocket .framing .CloseFrame ;
2829import org .java_websocket .framing .CloseFrameBuilder ;
2930import org .java_websocket .framing .Framedata ;
@@ -291,7 +292,7 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) throws IOException
291292 private void decodeFrames ( ByteBuffer socketBuffer ) {
292293 if ( flushandclosestate )
293294 return ;
294- assert ( isOpen () );
295+ assert ( ! isClosed () );
295296 List <Framedata > frames ;
296297 try {
297298 frames = draft .translateFrame ( socketBuffer );
@@ -316,7 +317,8 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
316317 // echo close handshake
317318 if ( draft .getCloseHandshakeType () == CloseHandshakeType .TWOWAY )
318319 close ( code , reason , true );
319- flushAndClose ( code , reason , false );
320+ else
321+ flushAndClose ( code , reason , false );
320322 }
321323 continue ;
322324 } else if ( curop == Opcode .PING ) {
@@ -355,7 +357,6 @@ private void decodeFrames( ByteBuffer socketBuffer ) {
355357 }
356358 }
357359
358-
359360 private void close ( int code , String message , boolean remote ) {
360361 if ( !closeHandshakeSubmitted ) {
361362 if ( handshakeComplete ) {
@@ -367,15 +368,15 @@ private void close( int code, String message, boolean remote ) {
367368 }
368369 if ( draft .getCloseHandshakeType () != CloseHandshakeType .NONE ) {
369370 try {
371+ if ( !remote )
372+ wsl .onWebsocketCloseInitiated ( this , code , message );
370373 sendFrame ( new CloseFrameBuilder ( code , message ) );
371- wsl .onWebsocketCloseInitiated ( this , code , message );
372374 } catch ( InvalidDataException e ) {
373375 wsl .onWebsocketError ( this , e );
374376 flushAndClose ( CloseFrame .ABNORMAL_CLOSE , "generated frame is invalid" , false );
375377 }
376- } else {
377- flushAndClose ( code , message , false );
378378 }
379+ flushAndClose ( code , message , remote );
379380 } else if ( code == CloseFrame .FLASHPOLICY ) {
380381 assert ( remote );
381382 flushAndClose ( CloseFrame .FLASHPOLICY , message , true );
@@ -425,13 +426,13 @@ protected synchronized void closeConnection( int code, String message, boolean r
425426 handshakerequest = null ;
426427
427428 isclosed = true ;
428-
429+
429430 }
430431
431432 protected void closeConnection ( int code , boolean remote ) {
432433 closeConnection ( code , "" , remote );
433434 }
434-
435+
435436 public void closeConnection () {
436437 if ( closedremotely == null ) {
437438 throw new IllegalStateException ( "this method must be used in conjuction with flushAndClose" );
@@ -455,7 +456,7 @@ protected synchronized void flushAndClose( int code, String message, boolean rem
455456
456457 wsl .onWriteDemand ( this ); // ensures that all outgoing frames are flushed before closing the connection
457458
458- this .wsl .onWebsocketCloseInitiated ( this , code , message );
459+ this .wsl .onWebsocketClosing ( this , code , message , remote );
459460 if ( draft != null )
460461 draft .reset ();
461462 tempContiniousFrame = null ;
@@ -494,7 +495,7 @@ public void close( InvalidDataException e ) {
494495 * @throws NotYetConnectedException
495496 */
496497 @ Override
497- public void send ( String text ) throws NotYetConnectedException {
498+ public void send ( String text ) throws WebsocketNotConnectedException {
498499 if ( text == null )
499500 throw new IllegalArgumentException ( "Cannot send 'null' data to a WebSocketImpl." );
500501 send ( draft .createFrames ( text , role == Role .CLIENT ) );
@@ -507,20 +508,20 @@ public void send( String text ) throws NotYetConnectedException {
507508 * @throws NotYetConnectedException
508509 */
509510 @ Override
510- public void send ( ByteBuffer bytes ) throws IllegalArgumentException , NotYetConnectedException {
511+ public void send ( ByteBuffer bytes ) throws IllegalArgumentException , WebsocketNotConnectedException {
511512 if ( bytes == null )
512513 throw new IllegalArgumentException ( "Cannot send 'null' data to a WebSocketImpl." );
513514 send ( draft .createFrames ( bytes , role == Role .CLIENT ) );
514515 }
515516
516517 @ Override
517- public void send ( byte [] bytes ) throws IllegalArgumentException , NotYetConnectedException {
518+ public void send ( byte [] bytes ) throws IllegalArgumentException , WebsocketNotConnectedException {
518519 send ( ByteBuffer .wrap ( bytes ) );
519520 }
520521
521522 private void send ( Collection <Framedata > frames ) {
522- if ( !this . handshakeComplete )
523- throw new NotYetConnectedException ();
523+ if ( !isOpen () )
524+ throw new WebsocketNotConnectedException ();
524525 for ( Framedata f : frames ) {
525526 sendFrame ( f );
526527 }
0 commit comments