How to correctly implement a spring-websocket java client

痞子三分冷 提交于 2020-01-01 04:43:11

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/28304384/how-to-correctly-implement-a-spring-websocket-java-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!