Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
Expand Down
55 changes: 55 additions & 0 deletions src/main/example/ReconnectClientExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2010-2018 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

import org.java_websocket.WebSocketImpl;

import java.net.URI;
import java.net.URISyntaxException;

/**
* Simple example to reconnect blocking and non-blocking.
*/
public class ReconnectClientExample {
public static void main( String[] args ) throws URISyntaxException, InterruptedException {
WebSocketImpl.DEBUG = true;
ExampleClient c = new ExampleClient( new URI( "ws://localhost:8887" ) );
//Connect to a server normally
c.connectBlocking();
c.send( "hi" );
Thread.sleep( 10 );
c.closeBlocking();
//Disconnect manually and reconnect blocking
c.reconnectBlocking();
c.send( "it's" );
Thread.sleep( 10000 );
c.closeBlocking();
//Disconnect manually and reconnect
c.reconnect();
Thread.sleep( 100 );
c.send( "me" );
Thread.sleep( 100 );
c.closeBlocking();
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,51 @@ public Socket getSocket() {
return socket;
}

/**
* Reinitiates the websocket connection. This method does not block.
* @since 1.3.8
*/
public void reconnect() {
reset();
connect();
}

/**
* Same as <code>reconnect</code> but blocks until the websocket reconnected or failed to do so.<br>
* @return Returns whether it succeeded or not.
* @throws InterruptedException Thrown when the threads get interrupted
* @since 1.3.8
*/
public boolean reconnectBlocking() throws InterruptedException {
reset();
return connectBlocking();
}

/**
* Reset everything relevant to allow a reconnect
*
* @since 1.3.8
*/
private void reset() {
close();
this.draft.reset();
if (this.socket != null) {
try {
this.socket.close();
} catch ( IOException e ) {
e.printStackTrace();
}
try {
this.socket = new Socket( this.socket.getInetAddress(), this.socket.getPort() );
} catch ( IOException e ) {
e.printStackTrace();
}
}
connectLatch = new CountDownLatch( 1 );
closeLatch = new CountDownLatch( 1 );
this.engine = new WebSocketImpl( this, this.draft );
}

/**
* Initiates the websocket connection. This method does not block.
*/
Expand Down Expand Up @@ -538,6 +583,7 @@ public void run() {
handleIOException( e );
} finally {
closeSocket();
writeThread = null;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/java_websocket/issues/AllIssueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
@Suite.SuiteClasses({
org.java_websocket.issues.Issue609Test.class,
org.java_websocket.issues.Issue621Test.class,
org.java_websocket.issues.Issue580Test.class
org.java_websocket.issues.Issue580Test.class,
org.java_websocket.issues.Issue256Test.class
})
/**
* Start all tests for issues
Expand Down
200 changes: 200 additions & 0 deletions src/test/java/org/java_websocket/issues/Issue256Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (c) 2010-2018 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/

package org.java_websocket.issues;

import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.util.SocketUtil;
import org.java_websocket.util.ThreadCheck;
import org.junit.Rule;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.URI;
import java.util.concurrent.CountDownLatch;

public class Issue256Test {

/**
* This causes problems on my jenkins. Need to fix this so just deactivate it for the pull request
@Rule
public ThreadCheck zombies = new ThreadCheck();

private void runTestScenarioReconnect(boolean reconnectBlocking) throws Exception {
final CountDownLatch countServerDownLatch = new CountDownLatch( 1 );
int port = SocketUtil.getAvailablePort();
WebSocketServer ws = new WebSocketServer( new InetSocketAddress( port ) ) {
@Override
public void onOpen( WebSocket conn, ClientHandshake handshake ) {

}

@Override
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {

}

@Override
public void onMessage( WebSocket conn, String message ) {

}

@Override
public void onError( WebSocket conn, Exception ex ) {

}

@Override
public void onStart() {
countServerDownLatch.countDown();
}
};
ws.start();
countServerDownLatch.await();
WebSocketClient clt = new WebSocketClient( new URI( "ws://localhost:" + port ) ) {
@Override
public void onOpen( ServerHandshake handshakedata ) {

}

@Override
public void onMessage( String message ) {

}

@Override
public void onClose( int code, String reason, boolean remote ) {

}

@Override
public void onError( Exception ex ) {

}
};
clt.connectBlocking();
clt.send("test");
clt.getSocket().close();
if (reconnectBlocking) {
clt.reconnectBlocking();
} else {
clt.reconnect();
}
Thread.sleep( 100 );

ws.stop();
Thread.sleep( 100 );
}

@Test
public void runReconnectBlockingScenario0() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario1() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario2() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario3() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario4() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario5() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario6() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario7() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario8() throws Exception {
runTestScenarioReconnect(true);
}
@Test
public void runReconnectBlockingScenario9() throws Exception {
runTestScenarioReconnect(true);
}

@Test
public void runReconnectScenario0() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario1() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario2() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario3() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario4() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario5() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario6() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario7() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario8() throws Exception {
runTestScenarioReconnect(false);
}
@Test
public void runReconnectScenario9() throws Exception {
runTestScenarioReconnect(false);
}

*/
}