Skip to content

Web Sockets

mega12345mega edited this page Jan 25, 2023 · 1 revision

Enabling Web Sockets

You must install TooTallNate's WebSocket library to allow this part of the library to function.

Enabling the web socket mode allows this library to communicate with the JS variant. Use Server#useWebSocket and Client#useWebSocket to enable this mode.

Example:

Server server = new Server(31415);
server.useWebSocket(true);
server.start();

Client webClient = new Client("ws://localhost", 31415);
webClient.useWebSocket(true);
webClient.start();

Client client = new Client(31415);
client.start(); // Will fail to connect, as the client defaults to the raw socket mode

Secure Web Sockets

Use Server#setSecure to add an SSL certificate to the server. If you have stored the certificate in a Java keystore, you can use Server.generateSSLContext to get it.

Example: (from Test #3)

FileInputStream stream = new FileInputStream(new File("src/test/keystore.jks"));
SSLContext ssl = Server.generateSSLContext(stream, "JKS", "storepassword", "keypassword", "SunX509");
stream.close();
Server server = new Server(31415).useWebSocket(true).setSecure(ssl);

Client client = new Client("wss://localhost", 31415).useWebSocket(true);

Clone this wiki locally