Skip to content

Commit 1a36700

Browse files
committed
merged @detro's pull request TooTallNate#74
2 parents 0374ba9 + 047c287 commit 1a36700

4 files changed

Lines changed: 105 additions & 32 deletions

File tree

‎src/org/java_websocket/WebSocket.java‎

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
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();

‎src/org/java_websocket/WebSocketClient.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ public void send( String text ) throws NotYetConnectedException , InterruptedExc
121121
}
122122
}
123123

124+
/**
125+
* Sends <var>data</var> to the connected WebSocket server.
126+
*
127+
* @param data
128+
* The Byte-Array of data to send to the WebSocket server.
129+
*/
130+
public void send( byte[] data ) throws NotYetConnectedException , InterruptedException {
131+
if( conn != null ) {
132+
conn.send( data );
133+
}
134+
}
135+
124136
private void tryToConnect( InetSocketAddress remote ) throws IOException {
125137
client = SocketChannel.open();
126138
client.configureBlocking( false );
@@ -251,6 +263,17 @@ private void sendHandshake() throws IOException , InvalidHandshakeException , In
251263
conn.startHandshake( handshake );
252264
}
253265

266+
/**
267+
* Retrieve the WebSocket 'readyState'.
268+
* This represents the state of the connection.
269+
* It returns a numerical value, as per W3C WebSockets specs.
270+
*
271+
* @return Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
272+
*/
273+
public int getReadyState() {
274+
return conn.getReadyState();
275+
}
276+
254277
/**
255278
* Calls subclass' implementation of <var>onMessage</var>.
256279
*

‎src/org/java_websocket/WebSocketListener.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public interface WebSocketListener {
5252
*
5353
* @param conn
5454
* The <tt>WebSocket</tt> instance this event is occurring on.
55-
* @param message
55+
* @param blob
5656
* The binary message that was received.
5757
*/
5858
public void onWebsocketMessage( WebSocket conn, byte[] blob );

‎src/org/java_websocket/WebSocketServer.java‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,28 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
5151
// CONSTRUCTORS ////////////////////////////////////////////////////////////
5252
/**
5353
* Nullary constructor. Creates a WebSocketServer that will attempt to
54-
* listen on port WebSocket.DEFAULT_PORT.
54+
* listen on port <var>WebSocket.DEFAULT_PORT</var>.
5555
*/
5656
public WebSocketServer() throws UnknownHostException {
57-
this( new InetSocketAddress( InetAddress.getLocalHost(), WebSocket.DEFAULT_PORT ) , null );
57+
this( new InetSocketAddress( InetAddress.getLocalHost(), WebSocket.DEFAULT_PORT ), null );
5858
}
5959

6060
/**
61-
* Creates a WebSocketServer that will attempt to listen on port
62-
* <var>port</var>.
61+
* Creates a WebSocketServer that will attempt to bind/listen on the given <var>address</var>.
6362
*
64-
* @param port
65-
* The port number this server should listen on.
63+
* @param address
64+
* The address (host:port) this server should listen on.
6665
*/
6766
public WebSocketServer( InetSocketAddress address ) {
6867
this( address, null );
6968
}
7069

7170
/**
72-
* Creates a WebSocketServer that will attempt to listen on port <var>port</var>,
71+
* Creates a WebSocketServer that will attempt to bind/listen on the given <var>address</var>,
7372
* and comply with <tt>Draft</tt> version <var>draft</var>.
7473
*
75-
* @param port
76-
* The port number this server should listen on.
74+
* @param address
75+
* The address (host:port) this server should listen on.
7776
* @param draft
7877
* The version of the WebSocket protocol that this server
7978
* instance should comply to.
@@ -87,7 +86,8 @@ public WebSocketServer( InetSocketAddress address , Draft draft ) {
8786
/**
8887
* Starts the server thread that binds to the currently set port number and
8988
* listeners for WebSocket connection requests.
90-
* @throws IllegalStateException
89+
*
90+
* @throws IllegalStateException
9191
*/
9292
public void start() {
9393
if( thread != null )
@@ -117,7 +117,7 @@ public void stop() throws IOException {
117117
*
118118
* @param text
119119
* The String to send across the network.
120-
* @throws IOException
120+
* @throws InterruptedException
121121
* When socket related I/O errors occur.
122122
*/
123123
public void sendToAll( String text ) throws InterruptedException {
@@ -180,10 +180,10 @@ public Set<WebSocket> connections() {
180180
}
181181

182182
/**
183-
* Sets the port that this WebSocketServer should listen on.
183+
* Sets the address (host:port) that this WebSocketServer should listen on.
184184
*
185-
* @param port
186-
* The port number to listen on.
185+
* @param address
186+
* The address (host:port) to listen on.
187187
*/
188188
public void setAddress( InetSocketAddress address ) {
189189
this.address = address;
@@ -192,7 +192,7 @@ public void setAddress( InetSocketAddress address ) {
192192
public InetSocketAddress getAddress() {
193193
return this.address;
194194
}
195-
195+
196196
/**
197197
* Gets the port number that this server listens on.
198198
*
@@ -202,7 +202,7 @@ public int getPort() {
202202
return getAddress().getPort();
203203
}
204204

205-
public Draft getDraft(){
205+
public Draft getDraft() {
206206
return this.draft;
207207
}
208208

@@ -215,7 +215,7 @@ public void run() {
215215
server = ServerSocketChannel.open();
216216
server.configureBlocking( false );
217217
server.socket().bind( address );
218-
//InetAddress.getLocalHost()
218+
// InetAddress.getLocalHost()
219219
selector = Selector.open();
220220
server.register( selector, server.validOps() );
221221
} catch ( IOException ex ) {

0 commit comments

Comments
 (0)