{#========================================== Spincast HTTP Client with WebSockets support plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins hasBreadCrumb plugins-spincast-http-client-with-websocket{% endblock %} {% block meta_title %}Plugins - Spincast HTTP Client with WebSockets support{% endblock %} {% block meta_description %}Client to easily create and send HTTP requests and establish WebSocket connections.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

This plugin is the same as the regular Spincast HTTP Client but adds support for WebSockets.

We decided to create two separate versions because this one uses classes from Undertow, so more transitive dependencies are pulled into an application using it. We wanted to give the possibility to use the HTTP Client without those extra dependencies, if preferred. But note that if you are already using the default Server in your Spincast application, it is also based on Undertow, so you already pull those dependencies...

All the WebSockets tests of Spincast use this HTTP client so have a look at them for a lot of examples!

Installation

Add this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-http-client-with-websocket</artifactId>
    <version>{{spincast.spincastCurrrentVersion}}</version>
</dependency>

Then, install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new AppModule(args),
        new SpincastHttpClientWithWebsocketPluginGuiceModule(AppRequestContext.class, 
                                                             AppWebsocketContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastDefaultGuiceModule {

    //...

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastHttpClientWithWebsocketPluginGuiceModule(getRequestContextType(), 
                                                                     getWebsocketContextType()));
        // other modules...
    }
    
    // ...
}

Usage

WebsocketClientHandler websocketHandler = new WebsocketClientHandler() {

    @Override
    public void onEndpointMessage(String message) {
        System.out.println("Message received from the server: " + message);
    }

    @Override
    public void onEndpointMessage(byte[] message) {
        System.out.println("Binary message received from the server...");
    }

    @Override
    public void onConnectionClosed(int code, String reason) {
        System.out.println("WebSocket connection closed.");
    }
};

HttpClient httpClient = getHttpClient();

WebsocketClientWriter websocketWriter =
        httpClient.websocket("http://example.com/someEndpoint")
                  .addCookie("cookieKey", "cookieValue")
                  .addHeaderValue("headerKey", "headerValue")
                  .connect(websocketHandler);

websocketWriter.sendMessage("Hi server!");

Explanation :

  • 1-17 : You define a WebSocket handler which is responsible for handling the WebSocket events.
  • 22-25 : You use the HTTP Client to connect to a WebSocket endpoint. Note that you can use all the utilities available from the HTTP core: addCookie(...), addHeader(...), etc.
  • 21 : A WebSocket writer is returned when to connection is established.
  • 27 : You use the writer to send message to the endpoint.

Configurations

You can bind the SpincastHttpClientWithWebsocketConfig interface if you want to change some default configurations. The default implementation class for those configurations is SpincastHttpClientWithWebsocketConfigDefault.

Javadoc

{% endblock %}