Skip to content

Commit da467db

Browse files
committed
Disallow reconnect
throw an exception if reconnect is called out of the connectRead or write thread
1 parent dfe3e21 commit da467db

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna
9393
*/
9494
private Thread writeThread;
9595

96+
/**
97+
* The thread to connect and read message
98+
*/
99+
private Thread connectReadThread;
100+
96101
/**
97102
* The draft to use
98103
*/
@@ -239,12 +244,20 @@ public boolean reconnectBlocking() throws InterruptedException {
239244
* @since 1.3.8
240245
*/
241246
private void reset() {
247+
Thread current = Thread.currentThread();
248+
if (current == writeThread || current == connectReadThread) {
249+
throw new IllegalStateException("You cannot initialize a reconnect out of the websocket thread. Use reconnect in another thread to insure a successful cleanup.");
250+
}
242251
try {
243252
closeBlocking();
244253
if( writeThread != null ) {
245254
this.writeThread.interrupt();
246255
this.writeThread = null;
247256
}
257+
if( connectReadThread != null ) {
258+
this.connectReadThread.interrupt();
259+
this.connectReadThread = null;
260+
}
248261
this.draft.reset();
249262
if( this.socket != null ) {
250263
this.socket.close();
@@ -264,11 +277,11 @@ private void reset() {
264277
* Initiates the websocket connection. This method does not block.
265278
*/
266279
public void connect() {
267-
if( writeThread != null )
280+
if( connectReadThread != null )
268281
throw new IllegalStateException( "WebSocketClient objects are not reuseable" );
269-
writeThread = new Thread( this );
270-
writeThread.setName( "WebSocketConnectReadThread-" + writeThread.getId() );
271-
writeThread.start();
282+
connectReadThread = new Thread( this );
283+
connectReadThread.setName( "WebSocketConnectReadThread-" + connectReadThread.getId() );
284+
connectReadThread.start();
272285
}
273286

274287
/**
@@ -316,7 +329,7 @@ public void closeBlocking() throws InterruptedException {
316329

317330
/**
318331
* Sends <var>text</var> to the connected websocket server.
319-
*
332+
*
320333
* @param text
321334
* The string which will be transmitted.
322335
*/
@@ -326,7 +339,7 @@ public void send( String text ) throws NotYetConnectedException {
326339

327340
/**
328341
* Sends binary <var> data</var> to the connected webSocket server.
329-
*
342+
*
330343
* @param data
331344
* The byte-Array of data to send to the WebSocket server.
332345
*/
@@ -411,8 +424,7 @@ public void run() {
411424
onError( e );
412425
engine.closeConnection( CloseFrame.ABNORMAL_CLOSE, e.getMessage() );
413426
}
414-
//I have no idea why this was added.
415-
//assert ( socket.isClosed() );
427+
connectReadThread = null;
416428
}
417429

418430
/**

0 commit comments

Comments
 (0)