Skip to content

Commit c33f20c

Browse files
committed
Move WebSocketListener.Draft into it's own source file, and rename to WebSocketDraft. Also added a couple new convenience constructors to both the Client and Server classes.
1 parent 7066b2a commit c33f20c

File tree

4 files changed

+59
-42
lines changed

4 files changed

+59
-42
lines changed

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import java.util.Set;
1616
import java.util.concurrent.LinkedBlockingQueue;
1717

18-
import net.tootallnate.websocket.WebSocketListener.Draft;
19-
2018
/**
2119
* The <tt>WebSocketClient</tt> is an abstract class that expects a valid
2220
* "ws://" URI to connect to. When connected, an instance recieves important
@@ -51,9 +49,9 @@ public abstract class WebSocketClient implements Runnable, WebSocketListener {
5149
*/
5250
private boolean running;
5351
/**
54-
* The Websocket mode this client is in.
52+
* The Draft of the WebSocket protocol the Client is adhering to.
5553
*/
56-
private Draft draft;
54+
private WebSocketDraft draft;
5755
/**
5856
* Number 1 used in handshake
5957
*/
@@ -67,14 +65,18 @@ public abstract class WebSocketClient implements Runnable, WebSocketListener {
6765
*/
6866
private byte[] key3 = null;
6967

70-
// CONSTRUCTOR /////////////////////////////////////////////////////////////
68+
// CONSTRUCTORS ////////////////////////////////////////////////////////////
69+
public WebSocketClient(URI serverURI) {
70+
this(serverURI, WebSocketDraft.AUTO);
71+
}
72+
7173
/**
7274
* Constructs a WebSocketClient instance and sets it to the connect to the
7375
* specified URI. The client does not attampt to connect automatically. You
7476
* must call <var>connect</var> first to initiate the socket connection.
7577
* @param serverUri The <tt>URI</tt> of the WebSocket server to connect to.
7678
*/
77-
public WebSocketClient(URI serverUri,Draft draft) {
79+
public WebSocketClient(URI serverUri, WebSocketDraft draft) {
7880
this.uri = serverUri;
7981
this.draft = draft;
8082
}
@@ -89,7 +91,7 @@ public URI getURI() {
8991
}
9092

9193
@Override
92-
public Draft getDraft() {
94+
public WebSocketDraft getDraft() {
9395
return this.draft;
9496
}
9597

@@ -179,7 +181,7 @@ public void run() {
179181
"Connection: Upgrade\r\n" +
180182
"Host: " + host + "\r\n" +
181183
"Origin: " + origin + "\r\n";
182-
if (this.draft == Draft.DRAFT76) {
184+
if (this.draft == WebSocketDraft.DRAFT76) {
183185
request += "Sec-WebSocket-Key1: " + this.generateKey() + "\r\n";
184186
request += "Sec-WebSocket-Key2: " + this.generateKey() + "\r\n";
185187
this.key3 = new byte[8];
@@ -257,31 +259,31 @@ private String generateKey() {
257259
public boolean onHandshakeRecieved(WebSocket conn, String handshake, byte[] reply) throws IOException, NoSuchAlgorithmException {
258260
// TODO: Do some parsing of the returned handshake, and close connection
259261
// (return false) if we recieved anything unexpected.
260-
if(this.draft == Draft.DRAFT76) {
262+
if(this.draft == WebSocketDraft.DRAFT76) {
261263
if (reply == null) {
262264
return false;
263265
}
264266
byte[] challenge = new byte[] {
265-
(byte)( this.number1 >> 24 ),
266-
(byte)( (this.number1 << 8) >> 24 ),
267-
(byte)( (this.number1 << 16) >> 24 ),
268-
(byte)( (this.number1 << 24) >> 24 ),
269-
(byte)( this.number2 >> 24 ),
270-
(byte)( (this.number2 << 8) >> 24 ),
271-
(byte)( (this.number2 << 16) >> 24 ),
272-
(byte)( (this.number2 << 24) >> 24 ),
273-
this.key3[0],
274-
this.key3[1],
275-
this.key3[2],
276-
this.key3[3],
277-
this.key3[4],
278-
this.key3[5],
279-
this.key3[6],
280-
this.key3[7]
267+
(byte)( this.number1 >> 24 ),
268+
(byte)( (this.number1 << 8) >> 24 ),
269+
(byte)( (this.number1 << 16) >> 24 ),
270+
(byte)( (this.number1 << 24) >> 24 ),
271+
(byte)( this.number2 >> 24 ),
272+
(byte)( (this.number2 << 8) >> 24 ),
273+
(byte)( (this.number2 << 16) >> 24 ),
274+
(byte)( (this.number2 << 24) >> 24 ),
275+
this.key3[0],
276+
this.key3[1],
277+
this.key3[2],
278+
this.key3[3],
279+
this.key3[4],
280+
this.key3[5],
281+
this.key3[6],
282+
this.key3[7]
281283
};
282284
MessageDigest md5 = MessageDigest.getInstance("MD5");
283285
byte[] expected = md5.digest(challenge);
284-
for(int i = 0; i < reply.length; i++){
286+
for (int i = 0; i < reply.length; i++) {
285287
if (expected[i] != reply[i]) {
286288
return false;
287289
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package net.tootallnate.websocket;
2+
3+
/**
4+
* Enum for WebSocket Draft
5+
*/
6+
public enum WebSocketDraft {
7+
AUTO,
8+
DRAFT75,
9+
DRAFT76
10+
}

src/net/tootallnate/websocket/WebSocketListener.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
* @author Nathan Rajlich
1010
*/
1111
interface WebSocketListener {
12-
/**
13-
* Enum for WebSocket Draft
14-
*/
15-
public static enum Draft {
16-
AUTO,
17-
DRAFT75,
18-
DRAFT76
19-
}
2012

2113
/**
2214
* Called when the socket connection is first established, and the WebSocket
@@ -58,5 +50,5 @@ public static enum Draft {
5850
/**
5951
* Called to retrieve the Draft of this listener.
6052
*/
61-
public Draft getDraft();
53+
public WebSocketDraft getDraft();
6254
}

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,37 @@ public abstract class WebSocketServer implements Runnable, WebSocketListener {
5252
*/
5353
private Selector selector;
5454
/**
55-
* The Draft of the WebSocket protocol the Server is running adhering to.
55+
* The Draft of the WebSocket protocol the Server is adhering to.
5656
*/
57-
private Draft draft;
58-
// CONSTRUCTOR /////////////////////////////////////////////////////////////
57+
private WebSocketDraft draft;
58+
59+
60+
// CONSTRUCTORS ////////////////////////////////////////////////////////////
5961
/**
6062
* Nullary constructor. Creates a WebSocketServer that will attempt to
6163
* listen on port WebSocket.DEFAULT_PORT.
6264
*/
6365
public WebSocketServer() {
64-
this(WebSocket.DEFAULT_PORT, Draft.AUTO);
66+
this(WebSocket.DEFAULT_PORT, WebSocketDraft.AUTO);
6567
}
66-
68+
6769
/**
6870
* Creates a WebSocketServer that will attempt to listen on port
6971
* <var>port</var>.
7072
* @param port The port number this server should listen on.
7173
*/
72-
public WebSocketServer(int port, Draft draft) {
74+
public WebSocketServer(int port) {
75+
this(port, WebSocketDraft.AUTO);
76+
}
77+
78+
/**
79+
* Creates a WebSocketServer that will attempt to listen on port <var>port</var>,
80+
* and comply with <tt>WebSocketDraft</tt> version <var>draft</var>.
81+
* @param port The port number this server should listen on.
82+
* @param draft The version of the WebSocket protocol that this server
83+
* instance should comply to.
84+
*/
85+
public WebSocketServer(int port, WebSocketDraft draft) {
7386
this.connections = new CopyOnWriteArraySet<WebSocket>();
7487
this.draft = draft;
7588
setPort(port);
@@ -169,7 +182,7 @@ public int getPort() {
169182
return this.port;
170183
}
171184

172-
public Draft getDraft() {
185+
public WebSocketDraft getDraft() {
173186
return this.draft;
174187
}
175188

0 commit comments

Comments
 (0)