如何正确实现spring-websocket java客户端 [英] How to correctly implement a spring-websocket java client

查看:1361
本文介绍了如何正确实现spring-websocket java客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的WebSocket服务器使用Spring WebSocket Stomp客户端,但是信息冲突. 我发现有两种方法可以使它正常工作,而又没有过多的细节,我想知道哪种方法被认为是实现客户端的正确"方法.

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.

有人可以帮助我了解WebSocketConnectionManager的用途吗?

Could somebody help me understand what the WebSocketConnectionManager is for?

还有一个问题,我如何保持websocket连接打开并且程序运行以接受新消息,而不必编写System.in.read()行.

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().

第一种方式:直接使用SockJsClient

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();

第二种方法:使用WebSocketConnectionManager

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();

我知道我可以通过使用@Configuration@Bean的注释来简化此操作,但是我正在尝试执行原始"实现,以便我可以理解所有功能如何协同工作.

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.

更多信息:

<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>

推荐答案

如果有趣的话,Spring Integration提供了

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

是的,内部使用ConnectionManagerSupport.

这是一个

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

但是我认为您应该尝试使用现成的WebSocketHandler实现-SubProtocolWebSocketHandlerStompSubProtocolHandler.

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

这篇关于如何正确实现spring-websocket java客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆