Skip to content

How to enable WSS

Thomas Volden edited this page Sep 14, 2017 · 11 revisions

How to enable WSS support

In order to create a secure channel, you have to provide the server with a certificate. Once this is done, you should be able to start the server as normal.

The following code is taken from a Java-Websocket example: https://github.com/TooTallNate/Java-WebSocket/blob/master/src/main/example/SSLServerExample.java `// load up the key store String STORETYPE = "JKS"; String KEYSTORE = "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 );

`

Once you have the certificate as a SSLContext, you can enable WSS.

I have modified the default example code to demonstrate (hightlighting the changed part with bold) ` server = new JSONServer(core); server.enableWSS(sslContext) server.open("localhost", 8887, new ServerEvents() {

        @Override
        public void newSession(UUID sessionIndex, SessionInformation information) {

            // sessionIndex is used to send messages.
            System.out.println("New session " + sessionIndex + ": " + information.getIdentifier());
        }

        @Override
        public void lostSession(UUID sessionIndex) {
        }
    });

`

Clone this wiki locally