Skip to content

Commit e5e2194

Browse files
committed
Make WebSocketClient#close actually end the underlying Thread and socket connection. Closes TooTallNate#5.
1 parent ca86db0 commit e5e2194

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

dist/WebSocket.jar

75 Bytes
Binary file not shown.

example/ChatClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void onClose() {
4343
private static class Frame extends JFrame implements ActionListener {
4444
private final JTextField uriField;
4545
private final JButton connect;
46+
private final JButton close;
4647
private final JTextArea area;
4748
private final JTextField chatField;
4849
private ChatClient cc;
@@ -52,7 +53,7 @@ public Frame() {
5253
Container c = getContentPane();
5354
GridLayout layout = new GridLayout();
5455
layout.setColumns(1);
55-
layout.setRows(4);
56+
layout.setRows(5);
5657
c.setLayout(layout);
5758

5859
uriField = new JTextField();
@@ -62,6 +63,10 @@ public Frame() {
6263
connect = new JButton("Connect");
6364
connect.addActionListener(this);
6465
c.add(connect);
66+
67+
close = new JButton("Close");
68+
close.addActionListener(this);
69+
c.add(close);
6570

6671
JScrollPane scroll = new JScrollPane();
6772
area = new JTextArea();
@@ -116,6 +121,12 @@ public void actionPerformed(ActionEvent e) {
116121
connect.setEnabled(true);
117122
uriField.setEditable(true);
118123
}
124+
} else if (e.getSource() == close) {
125+
try {
126+
cc.close();
127+
} catch(Exception ex) {
128+
ex.printStackTrace();
129+
}
119130
}
120131
}
121132
}

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,26 @@ public abstract class WebSocketClient implements Runnable, WebSocketListener {
4646
* The 'Selector' used to get event keys from the underlying socket.
4747
*/
4848
private Selector selector;
49+
/**
50+
* Keeps track of whether or not the client thread should continue running.
51+
*/
52+
private boolean running;
4953
/**
5054
* The Websocket mode this client is in.
5155
*/
5256
private Draft draft;
5357
/**
5458
* Number 1 used in handshake
5559
*/
56-
private int number1=0;
60+
private int number1 = 0;
5761
/**
5862
* Number 2 used in handshake
5963
*/
60-
private int number2=0;
64+
private int number2 = 0;
6165
/**
6266
* Key3 used in handshake
6367
*/
64-
private byte[] key3=null;
65-
public static enum Draft{
66-
DRAFT75,
67-
DRAFT76
68-
}
68+
private byte[] key3 = null;
6969

7070
// CONSTRUCTOR /////////////////////////////////////////////////////////////
7171
/**
@@ -87,13 +87,19 @@ public WebSocketClient(URI serverUri,Draft draft) {
8787
public URI getURI() {
8888
return uri;
8989
}
90+
91+
@Override
92+
public Draft getDraft() {
93+
return this.draft;
94+
}
9095

9196
/**
9297
* Starts a background thread that attempts and maintains a WebSocket
9398
* connection to the URI specified in the constructor or via <var>setURI</var>.
9499
* <var>setURI</var>.
95100
*/
96101
public void connect() {
102+
this.running = true;
97103
(new Thread(this)).start();
98104
}
99105

@@ -103,6 +109,8 @@ public void connect() {
103109
* @throws IOException When socket related I/O errors occur.
104110
*/
105111
public void close() throws IOException {
112+
this.running = false;
113+
selector.wakeup();
106114
conn.close();
107115
}
108116

@@ -141,7 +149,7 @@ public void run() {
141149
}
142150

143151
// Continuous loop that is only supposed to end when "close" is called.
144-
while (true) {
152+
while (this.running) {
145153
try {
146154
selector.select();
147155
Set<SelectionKey> keys = selector.selectedKeys();
@@ -197,7 +205,7 @@ public void run() {
197205
}
198206
}
199207

200-
//System.err.println("WebSocketClient thread ended!");
208+
System.err.println("WebSocketClient thread ended!");
201209
}
202210

203211
private String generateKey() {
@@ -306,11 +314,6 @@ public void onOpen(WebSocket conn) {
306314
public void onClose(WebSocket conn) {
307315
onClose();
308316
}
309-
310-
@Override
311-
public net.tootallnate.websocket.WebSocketListener.Draft getDraft() {
312-
return (net.tootallnate.websocket.WebSocketListener.Draft)net.tootallnate.websocket.WebSocketListener.Draft.valueOf(this.draft.name());
313-
}
314317

315318
// ABTRACT METHODS /////////////////////////////////////////////////////////
316319
public abstract void onMessage(String message);

src/net/tootallnate/websocket/WebSocketServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ public void setPort(int port) {
166166
* @return The port number.
167167
*/
168168
public int getPort() {
169-
return port;
169+
return this.port;
170170
}
171171

172172
public Draft getDraft() {
173-
return draft;
173+
return this.draft;
174174
}
175175

176176

0 commit comments

Comments
 (0)