This plugin is the same as the Spincast HTTP Client plugin 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 anyway...
All the WebSockets tests of Spincast use this HTTP client so have a look at them for a lot of examples!
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") .setCookie("cookieKey", "cookieValue") .setHeader("headerKey", "headerValue") .connect(websocketHandler); websocketWriter.sendMessage("Hi server!");
Explanation :
WebSocket handler
which is responsible for handling the WebSocket events.
HTTP Client
to connect to
a WebSocket endpoint. Note that you can use all the
utilities available from the HTTP core: addCookie(...)
,
addHeader(...)
, etc.
WebSocket writer
is returned when
to connection is established.
writer
to send message to the endpoint.
You can bind the SpincastHttpClientWithWebsocketConfig interface if you want to change some default configurations. The default implementation class for those configurations is SpincastHttpClientWithWebsocketConfigDefault.
httpClient()
Route Handlers
.
Learn how to install an add-on in the dedicated section of the documentation.
1. Add this Maven artifact to your project:
<dependency> <groupId>org.spincast</groupId> <artifactId>spincast-plugins-http-client-with-websocket</artifactId> <version>2.2.0</version> </dependency>
2. Add an instance of the SpincastHttpClientWithWebsocketPlugin plugin to your Spincast Bootstrapper:
Spincast.configure() .plugin(new SpincastHttpClientWithWebsocketPlugin()) // ...
The class implementing the SpincastPlugin interface is SpincastHttpClientWithWebsocketPlugin.