Skip to content

Commit f0ce0d9

Browse files
committed
1 parent c5e3c81 commit f0ce0d9

1 file changed

Lines changed: 11 additions & 22 deletions

File tree

src/org/java_websocket/client/WebSocketClient.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.net.URI;
77
import java.nio.ByteBuffer;
88
import java.nio.channels.ByteChannel;
9+
import java.nio.channels.CancelledKeyException;
910
import java.nio.channels.ClosedByInterruptException;
1011
import java.nio.channels.NotYetConnectedException;
1112
import java.nio.channels.SelectionKey;
@@ -16,8 +17,6 @@
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Set;
19-
import java.util.concurrent.locks.Lock;
20-
import java.util.concurrent.locks.ReentrantLock;
2120

2221
import org.java_websocket.SocketChannelIOHelper;
2322
import org.java_websocket.WebSocket;
@@ -68,8 +67,6 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
6867

6968
private Draft draft;
7069

71-
private final Lock closelock = new ReentrantLock();
72-
7370
private Map<String,String> headers;
7471

7572
WebSocketClientFactory wf = new WebSocketClientFactory() {
@@ -142,14 +139,14 @@ public void connect() {
142139

143140
public void close() {
144141
if( thread != null ) {
145-
thread.interrupt();
146-
closelock.lock();
142+
conn.close( CloseFrame.NORMAL );
143+
/*closelock.lock();
147144
try {
148145
if( selector != null )
149146
selector.wakeup();
150147
} finally {
151148
closelock.unlock();
152-
}
149+
}*/
153150
}
154151

155152
}
@@ -191,23 +188,16 @@ public void run() {
191188
if( thread == null )
192189
thread = Thread.currentThread();
193190
interruptableRun();
191+
192+
assert ( !channel.isOpen() );
194193

195194
try {
196195
if( selector != null ) // if the initialization in <code>tryToConnect</code> fails, it could be null
197196
selector.close();
198197
} catch ( IOException e ) {
199198
onError( e );
200199
}
201-
closelock.lock();
202-
selector = null;
203-
closelock.unlock();
204-
try {
205-
channel.close();
206-
} catch ( IOException e ) {
207-
onError( e );
208-
}
209-
channel = null;
210-
thread = null;
200+
211201
}
212202

213203
private final void interruptableRun() {
@@ -230,9 +220,6 @@ private final void interruptableRun() {
230220
ByteBuffer buff = ByteBuffer.allocate( WebSocket.RCVBUF );
231221
try/*IO*/{
232222
while ( channel.isOpen() ) {
233-
if( Thread.interrupted() ) {
234-
conn.close( CloseFrame.NORMAL );
235-
}
236223
SelectionKey key = null;
237224
selector.select();
238225
Set<SelectionKey> keys = selector.selectedKeys();
@@ -271,6 +258,8 @@ private final void interruptableRun() {
271258
}
272259
}
273260
}
261+
262+
} catch ( CancelledKeyException e ) {
274263
} catch ( IOException e ) {
275264
onError( e );
276265
conn.close( CloseFrame.ABNORMAL_CLOSE );
@@ -303,9 +292,9 @@ private void finishConnect( SelectionKey key ) throws IOException , InvalidHands
303292
channel.finishConnect();
304293
}
305294
// Now that we're connected, re-register for only 'READ' keys.
306-
key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
295+
conn.key = key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
307296

308-
wrappedchannel = wf.wrapChannel( key, uri.getHost(), getPort() );
297+
conn.channel = wrappedchannel = wf.wrapChannel( key, uri.getHost(), getPort() );
309298
sendHandshake();
310299
}
311300

0 commit comments

Comments
 (0)