Websocket java 客户端 Spring + Stomp:传输错误:ConnectionLostException [英] Websocket java client Spring + Stomp: Transport error: ConnectionLostException

查看:167
本文介绍了Websocket java 客户端 Spring + Stomp:传输错误:ConnectionLostException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到 spring 规范和 spring-portafolio 示例,我正在尝试使用 Stomp 和 Sockjs 创建一个独立的 Java 应用程序作为用于此 Spring 的 websocket 客户端,但出现此错误:

I'm trying to create an standalone Java app as a websocket client using for this Spring with Stomp and Sockjs, taking in consideration the spring specifications and the spring-portafolio examples and I'm getting this error:

15:18:01.342 [main] DEBUG com.example.client.WebSocketClientTest - Connecting to : ws://localhost:8080/socket/hello
15:18:01.541 [WebSocketClient-AsyncIO-1] ERROR com.example.client.MyStompSessionHandler - Transport error
org.springframework.messaging.simp.stomp.ConnectionLostException: Connection closed
at org.springframework.messaging.simp.stomp.DefaultStompSession.afterConnectionClosed(DefaultStompSession.java:483) [spring-messaging-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.socket.messaging.WebSocketStompClient$WebSocketTcpConnectionHandlerAdapter.afterConnectionClosed(WebSocketStompClient.java:354) [spring-websocket-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.socket.sockjs.client.AbstractClientSockJsSession.afterTransportClosed(AbstractClientSockJsSession.java:321) [spring-websocket-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.socket.sockjs.client.WebSocketTransport$ClientSockJsWebSocketHandler.afterConnectionClosed(WebSocketTransport.java:172) [spring-websocket-4.3.2.RELEASE.jar:4.3.2.RELEASE]
at org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter.onClose(StandardWebSocketHandlerAdapter.java:141) [spring-websocket-4.3.2.RELEASE.jar:4.3.2.RELEASE]...

我在java客户端的代码是:

My code in the java client side is:

StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
List<Transport> transports = new ArrayList<>();
transports.add(new WebSocketTransport(webSocketClient));
SockJsClient sockJsClient = new SockJsClient(transports);
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.afterPropertiesSet();
String stompUrl = "ws://localhost:8080/socket/hello";
WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setMessageConverter(new StringMessageConverter());
stompClient.setTaskScheduler(taskScheduler);
stompClient.setDefaultHeartbeat(new long[] {0, 0});

StompSessionHandler sessionHandler = new MyStompSessionHandler();
stompClient.connect(stompUrl, sessionHandler);
stompClient.setTaskScheduler(taskScheduler);

服务器端是使用带有 Stomp 和 SockJs 的 Spring MVC 制作的,服务器与 javascript 客户端完美配合,这是我使用的配置:

And the server side was made using Spring MVC with Stomp and SockJs, the server works perfectly with a javascript client, this is the config that I used:

public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/hello")
            .setHandshakeHandler(new DefaultHandshakeHandler(new TomcatRequestUpgradeStrategy()))
            .withSockJS();
}

我做错了什么?任何人都可以告诉我如何修复它或如何创建一个 Java 客户端来连接 Spring websocket 服务器吗?

What am I doing wrong? Can anyone give me an idea of how to fix it or how to create a Java client to connect with an spring websocket server?

提前致谢..

推荐答案

我遇到了同样的问题.原因是收到的消息太大,默认 STOMP Spring/Tomcat WS 客户端无法处理,因为它不能进行部分消息.这个 StackOverflow 答案 对我有用(我已经设置了 MAX_TEXT_MESSAGE_BUFFER_SIZE=20*1024*1024):

I had the same problem. Cause was recieving too big message which default STOMP Spring/Tomcat WS client cannot handle because it is not capable of partial messaging. This StackOverflow answer worked for me (I have set MAX_TEXT_MESSAGE_BUFFER_SIZE=20*1024*1024):

WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.setDefaultMaxTextMessageBufferSize(MAX_TEXT_MESSAGE_BUFFER_SIZE);
WebSocketClient wsClient = new StandardWebSocketClient(container);

设置 inboundMessageSizeLimit 无效:

Setting inboundMessageSizeLimit had no effect:

WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);
stompClient.setInboundMessageSizeLimit(Integer.MAX_VALUE);

这篇关于Websocket java 客户端 Spring + Stomp:传输错误:ConnectionLostException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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