WebSocket Support with CoralReactor

CoralReactor supports WebSockets out-of-the-box. In this article we present a simple CoralReactor websocket client that connects to an echo websocket server.

The Echo Server

The site www.websocket.org contains a test echo server at ws://echo.websocket.org that you can use to test your websocket clients. To create a CoralReactor websocket client you just inherit from AbstractWebSocketClient as the example below demonstrates:

package com.coralblocks.coralreactor.client.ws;

import static com.coralblocks.corallog.Log.*;

import java.nio.ByteBuffer;

import com.coralblocks.coralreactor.client.Client;
import com.coralblocks.coralreactor.nio.NioReactor;
import com.coralblocks.coralreactor.util.Configuration;

public class SimpleWebSocketClient extends AbstractWebSocketClient {

	public SimpleWebSocketClient(NioReactor nio, String host, int port) {
	    super(nio, host, port);
    }
	
	public SimpleWebSocketClient(NioReactor nio, String host, int port, Configuration config) {
		super(nio, host, port, config);
	}
	
	@Override
	protected void handleConnectionOpened() {
		setEventTimeout(3000);
	}
	
	@Override
	protected void handleConnectionTerminated() {
		setEventTimeout(0);
	}
	
	@Override
	protected void handleEventTimeout(long now, int timeout) {
		send("Hello from CoralBlocks!");
		if (isConnectionOpen()) setEventTimeout(timeout);
	}
	
	@Override
	protected void handleMessage(ByteBuffer msg) {
		Info.log(name, "Got message!", "len=", msg.remaining(), "contents=", msg);
	}
	
	public static void main(String[] args) throws Exception {
		NioReactor nio = NioReactor.create();
		Client client = new SimpleWebSocketClient(nio, "echo.websocket.org", 80);
		client.open();
		nio.start();
	}
}

The output:

08:20:19.308599-INFO SimpleWebSocketClient-echo.websocket.org:80 Client opened! sequence=1
08:20:19.328736-INFO NioReactor Reactor started! type=OptimumNioReactor impl=KQueueSelectorImpl
08:20:19.587244-INFO SimpleWebSocketClient-echo.websocket.org:80 Connection established!
08:20:19.616175-INFO SimpleWebSocketClient-echo.websocket.org:80 Connection opened!
08:20:22.669745-INFO SimpleWebSocketClient-echo.websocket.org:80 Got message! len=23 contents=Hello from CoralBlocks!
08:20:25.647737-INFO SimpleWebSocketClient-echo.websocket.org:80 Got message! len=23 contents=Hello from CoralBlocks!
08:20:28.666637-INFO SimpleWebSocketClient-echo.websocket.org:80 Got message! len=23 contents=Hello from CoralBlocks!


SSL Support (WSS)

CoralReactor supports SSL security out-of-the-box. You can read more about it in this article. The echo.websocket.org server also supports SSL at wss://echo.websocket.org (i.e. port 443 instead of 80). Below we show how easy it is to make the CoralReactor websocket client above use SSL:

	public static void main(String[] args) throws Exception {
		
		NioReactor nio = NioReactor.create();
		MapConfiguration config = new MapConfiguration();
		config.add("useSSL", true); // tell CoralReactor that we want to use SSL
		
		Client client = new SimpleWebSocketClient(nio, "echo.websocket.org", 443, config); // NOTE: 443 for SSL
		client.open();
		nio.start();
	}

The output:

08:36:30.442509-INFO SimpleWebSocketClient-echo.websocket.org:443 Client opened! sequence=1
08:36:31.209797-INFO NioReactor Reactor started! type=OptimumNioReactor impl=KQueueSelectorImpl
08:36:31.401301-INFO SimpleWebSocketClient-echo.websocket.org:443 Connection established!
08:36:31.539130-INFO SimpleWebSocketClient-echo.websocket.org:443 Connection opened!
08:36:34.601468-INFO SimpleWebSocketClient-echo.websocket.org:443 Got message! len=23 contents=Hello from CoralBlocks!
08:36:37.570590-INFO SimpleWebSocketClient-echo.websocket.org:443 Got message! len=23 contents=Hello from CoralBlocks!
08:36:40.575693-INFO SimpleWebSocketClient-echo.websocket.org:443 Got message! len=23 contents=Hello from CoralBlocks!