Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReconnectClientExample does not work with wss #825

Closed
AlexisBrito opened this issue Dec 12, 2018 · 5 comments
Closed

ReconnectClientExample does not work with wss #825

AlexisBrito opened this issue Dec 12, 2018 · 5 comments

Comments

@AlexisBrito
Copy link

If I merge the SSLClientExample.java and ReconnectClientExample.java, the program will end after it closes the connection with the server, not having a chance to do reconnectBlocking();

Starting from the SSLClientExample.java, after the c.connectBlocking();, if I copy and paste this:

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();

It will only display "hi", but it should display "hi it's me".

@marci4
Copy link
Collaborator

marci4 commented Dec 17, 2018

Hello @AlexisBrito,

can your issue be related to #764 ?

Best regards,
Marcel

@marci4
Copy link
Collaborator

marci4 commented Dec 24, 2018

@AlexisBrito any update?

@AlexisBrito
Copy link
Author

Hi @marci4,

Sorry for the delayed reply. I'm not sure it can, since in my code, I'm already creating a socket factory...

I can't really look into this at the moment so I'm going to share the whole code and hopefully you know where the issue is (if there is any):

public class SSLClientExample {

/*
 * Keystore with certificate created like so (in JKS format):
 *
 *keytool -genkey -keyalg RSA -validity 3650 -keystore "keystore.jks" -storepass "storepassword" -keypass "keypassword" -alias "default" -dname "CN=127.0.0.1, OU=MyOrgUnit, O=MyOrg, L=MyCity, S=MyRegion, C=MyCountry"
 */
public static void main( String[] args ) throws Exception {
	WebSocketChatClient c = new WebSocketChatClient( new URI( "wss://localhost:8887" ) );

	// load up the key store
	String STORETYPE = "JKS";
	String KEYSTORE = "C:\\Users\\alexandre.brito\\Documents\\NetBeansProjects\\Java-WebSocket-master2\\target\\keystore.jks";
	String STOREPASSWORD = "storepassword";
	String KEYPASSWORD = "keypassword";

	KeyStore ks = KeyStore.getInstance( STORETYPE );
	File kf = new File( KEYSTORE );
	ks.load( new FileInputStream( kf ), STOREPASSWORD.toCharArray() );

	KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
	kmf.init( ks, KEYPASSWORD.toCharArray() );
	TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
	tmf.init( ks );

	SSLContext sslContext = null;
	sslContext = SSLContext.getInstance( "TLS" );
	sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
	// sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates

	SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();

	c.setSocketFactory( factory );

            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();

	BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
	while ( true ) {
		String line = reader.readLine();
		if( line.equals( "close" ) ) {
			c.close();
		} else {
			c.send( line );
		}
	}
}

}

marci4 added a commit to marci4/Java-WebSocket-Dev that referenced this issue Dec 27, 2018
@marci4 marci4 mentioned this issue Dec 27, 2018
8 tasks
marci4 added a commit to marci4/Java-WebSocket-Dev that referenced this issue Dec 27, 2018
Remove system exit causing TooTallNate#825
@marci4
Copy link
Collaborator

marci4 commented Dec 27, 2018

Hey @AlexisBrito,

It simple does not work since there is a System.exit(0) when the client disconnects from the server (see https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/SSLClientExample.java#L63).

Best regards,
marci4

@AlexisBrito
Copy link
Author

Hey @marci4,

Thanks a lot! It's working :D

Best regards!

@marci4 marci4 closed this as completed Dec 28, 2018
marci4 added a commit that referenced this issue Jan 1, 2019
@NoahAndrews NoahAndrews mentioned this issue Apr 25, 2019
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants