Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement a spring-websocket java client

I am working on a Spring WebSocket Stomp Client for my WebSocket Server and I am getting conflicting information. I have found 2 ways to get it to work and without going into too much detail I was wondering which way is considered the "correct" way of implementing the client.

Could somebody help me understand what the WebSocketConnectionManager is for?

Also, one more question, how do I keep the websocket connection open and the program running to accept new messages without having to write the line System.in.read().

1'st way: Using the SockJsClient directly

URI uri = new URI("ws://localhost:8080/stomp");
StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();

List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

StompMessageReceiver messageHandler = new StompMessageReceiver();
StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter());

try {
    this.webSocketClient.doHandshake(websocketHandler, null, uri).get();
} catch (InterruptedException | ExecutionException e) {
    throw new IllegalStateException(e);
}

System.in.read();

2'nd way: Using the WebSocketConnectionManager

StandardWebSocketClient simpleWebSocketClient = new StandardWebSocketClient();
List<Transport> transports = new ArrayList<>(1);
transports.add(new WebSocketTransport(simpleWebSocketClient));

SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

StompMessageHandler messageHandler = new StompMessageHandler();
StompWebSocketHandler websocketHandler = new StompWebSocketHandler(messageHandler, new StringMessageConverter());

WebSocketConnectionManager manager = new WebSocketConnectionManager(sockJsClient, websocketHandler, "ws://localhost:8080/stomp");

manager.start();

System.in.read();

I know that I could make this a lot simpler by using the Annotations for @Configuration and @Bean but I am trying to do a "raw" implementation so I can understand how everything works together.

A little more information:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-client-api</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-websocket</artifactId>
    <version>8.0.0-RC10</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.5.0</version>
</dependency>
like image 939
Kevin - NOAA Affiliate Avatar asked Feb 03 '15 16:02

Kevin - NOAA Affiliate


People also ask

How do you use WebSockets in Spring?

In order to tell Spring to forward client requests to the endpoint , we need to register the handler. Start the application- Go to http://localhost:8080 Click on start new chat it opens the WebSocket connection. Type text in the textbox and click send. On clicking end chat, the WebSocket connection will be closed.

How do you implement WebSockets?

How Websockets are implemented? webSockets are implemented as follows: Client makes HTTP request to server with "upgrade" header on the request. If server agrees to the upgrade, then client and server exchange some security credentials and the protocol on the existing TCP socket is switched from HTTP to webSocket.


1 Answers

If it is interesting Spring Integration provides an implementation for the WebSocketClient.

And yes, internally it uses ConnectionManagerSupport.

Here is a test-case which demonstrate how to configure it from @Configuration.

But I think you should try with out-of-the-box WebSocketHandler implementation - SubProtocolWebSocketHandler, and StompSubProtocolHandler.

like image 179
Artem Bilan Avatar answered Sep 29 '22 12:09

Artem Bilan