2525/**
2626 * Represents one end (client or server) of a single WebSocket connection.
2727 * Takes care of the "handshake" phase, then allows for easy sending of
28- * text frames, and recieving frames through an event-based model.
28+ * text frames, and receiving frames through an event-based model.
2929 *
3030 * This is an inner class, used by <tt>WebSocketClient</tt> and <tt>WebSocketServer</tt>, and should never need to be instantiated directly
3131 * by your code. However, instances are exposed in <tt>WebSocketServer</tt> through the <i>onClientOpen</i>, <i>onClientClose</i>,
@@ -38,6 +38,12 @@ public final class WebSocket {
3838 public enum Role {
3939 CLIENT , SERVER
4040 }
41+
42+ public static final int READY_STATE_CONNECTING = 0 ;
43+ public static final int READY_STATE_OPEN = 1 ;
44+ public static final int READY_STATE_CLOSING = 2 ;
45+ public static final int READY_STATE_CLOSED = 3 ;
46+
4147 /**
4248 * The default port of WebSockets, as defined in the spec. If the nullary
4349 * constructor is used, DEFAULT_PORT will be the port the WebSocketServer
@@ -48,14 +54,19 @@ public enum Role {
4854 public static /*final*/ boolean DEBUG = false ; // must be final in the future in order to take advantage of VM optimization
4955
5056 /**
51- * Internally used to determine whether to receive data as part of the
52- * remote handshake, or as part of a text frame.
57+ * Determines whether to receive data as part of the
58+ * handshake, or as part of text/data frame transmitted over the websocket .
5359 */
5460 private boolean handshakeComplete = false ;
55-
61+ /**
62+ * Determines whether we sent already a request to Close the connection or not.
63+ */
5664 private boolean closeHandshakeSent = false ;
65+ /**
66+ * Determines wheter the connection is open or not
67+ */
5768 private boolean connectionClosed = false ;
58- private boolean isClosePending = false ;
69+
5970 /**
6071 * The listener to notify of WebSocket events.
6172 */
@@ -93,11 +104,11 @@ public enum Role {
93104 * The {@link WebSocketListener} to notify of events when
94105 * they occur.
95106 */
96- public WebSocket ( WebSocketListener listener , Draft draft , SocketChannel sockchannel ) {
97- init ( listener , draft , sockchannel );
107+ public WebSocket ( WebSocketListener listener , Draft draft , SocketChannel socketChannel ) {
108+ init ( listener , draft , socketChannel );
98109 }
99110
100- public WebSocket ( WebSocketListener listener , List <Draft > drafts , SocketChannel sockchannel ) {
111+ public WebSocket ( WebSocketListener listener , List <Draft > drafts , SocketChannel socketChannel ) {
101112 init ( listener , null , sockchannel );
102113 this .role = Role .SERVER ;
103114 if ( known_drafts == null || known_drafts .isEmpty () ) {
@@ -387,18 +398,25 @@ public void close( InvalidDataException e ) {
387398 }
388399
389400 /**
390- * @return True if all of the text was sent to the client by this thread or the given data is empty
391- * False if some of the text had to be buffered to be sent later.
392- * @throws IOException
401+ * Send Text data to the other end.
402+ *
403+ * @throws IllegalArgumentException
393404 * @throws InterruptedException
405+ * @throws NotYetConnectedException
394406 */
395407 public void send ( String text ) throws IllegalArgumentException , NotYetConnectedException , InterruptedException {
396408 if ( text == null )
397409 throw new IllegalArgumentException ( "Cannot send 'null' data to a WebSocket." );
398410 send ( draft .createFrames ( text , role == Role .CLIENT ) );
399411 }
400412
401- // TODO there should be a send for bytebuffers
413+ /**
414+ * Send Binary data (plain bytes) to the other end.
415+ *
416+ * @throws IllegalArgumentException
417+ * @throws InterruptedException
418+ * @throws NotYetConnectedException
419+ */
402420 public void send ( byte [] bytes ) throws IllegalArgumentException , NotYetConnectedException , InterruptedException {
403421 if ( bytes == null )
404422 throw new IllegalArgumentException ( "Cannot send 'null' data to a WebSocket." );
@@ -430,8 +448,7 @@ boolean hasBufferedData() {
430448 }
431449
432450 /**
433- * @return True if all data has been sent to the client, false if there
434- * is still some buffered.
451+ * Empty the internal buffer, sending all the pending data before continuing.
435452 */
436453 public void flush () throws IOException {
437454 ByteBuffer buffer = this .bufferQueue .peek ();
@@ -521,10 +538,43 @@ public InetSocketAddress getLocalSocketAddress() {
521538 return (InetSocketAddress ) sockchannel .socket ().getLocalSocketAddress ();
522539 }
523540
541+ public boolean isConnecting () {
542+ return ( !connectionClosed && !closeHandshakeSent && !handshakeComplete );
543+ }
544+
545+ public boolean isOpen () {
546+ return ( !connectionClosed && !closeHandshakeSent && handshakeComplete );
547+ }
548+
549+ public boolean isClosing () {
550+ return ( !connectionClosed && closeHandshakeSent );
551+ }
552+
524553 public boolean isClosed () {
525554 return connectionClosed ;
526555 }
527556
557+ /**
558+ * Retrieve the WebSocket 'readyState'.
559+ * This represents the state of the connection.
560+ * It returns a numerical value, as per W3C WebSockets specs.
561+ *
562+ * @return Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
563+ */
564+ public int getReadyState () {
565+ if ( isConnecting () ) {
566+ return READY_STATE_CONNECTING ;
567+ } else if ( isOpen () ) {
568+ return READY_STATE_OPEN ;
569+ } else if ( isClosing () ) {
570+ return READY_STATE_CLOSING ;
571+ } else if ( isClosed () ) {
572+ return READY_STATE_CLOSED ;
573+ }
574+ assert ( false );
575+ return -1 ; // < This can't happen, by design!
576+ }
577+
528578 @ Override
529579 public int hashCode () {
530580 return super .hashCode ();
0 commit comments