Java SockJS Spring客户端和消息大小 [英] Java SockJS Spring client and message size

查看:399
本文介绍了Java SockJS Spring客户端和消息大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SockJS Java客户端,我试图连接到Spring sockjs服务器,并且收到消息(无标题)约20Kb的错误1009. Javascript库工作正常.

Using SockJS java client, I am trying to connect to Spring sockjs server and is getting error 1009 for messages (without headers) of ~20Kb. Javascript library works fine.

Transport closed with CloseStatus[code=1009, reason=The decoded text message was too big 
for the output buffer and the endpoint does not support partial messages] in WebSocketClientSockJsSession
[id='9fa30eb453e14a8c8612e1064640646a, url=ws://127.0.0.1:8083/user]

我在服务器上有几个配置类(目前我不知道是否配置这些东西的次数超出了必要):

I have couple of config classes on server (I do not know at the moment if I am configuring these things more times than necessary):

@Configuration
@EnableWebSocket
public class WebSocketTransportConfig implements WebSocketConfigurer {
    // Important web socket setup. If big message is coming through, it may overflow the buffer and this will lead in disconnect.
    // All messages that are coming through normally (including snapshots) must be order of magnitude smaller, or connection will be broken
    // sometimes
    // There is also MaxBinaryMessageSize that we do not employ as we use Stomp, but for completeness it is also set to same values.
    // Javadoc http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/socket/adapter/jetty/JettyWebSocketSession.html#setTextMessageSizeLimit-int-

    public static final int MAX_TEXT_MESSAGE_SIZE = 2048000; // 2 Megabytes.
    public static final int BUFFER_SIZE = MAX_TEXT_MESSAGE_SIZE * 5;


    private static final Logger LOGGER = LogManager.getLogger(WebSocketTransportConfig.class);

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

    }

    @Bean
    public DefaultHandshakeHandler handshakeHandler() {
        LOGGER.info("Websocket buffer size: " + BUFFER_SIZE + " bytes.");
        WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
        policy.setMaxTextMessageBufferSize(BUFFER_SIZE);
        policy.setMaxTextMessageSize(MAX_TEXT_MESSAGE_SIZE);
        policy.setMaxBinaryMessageBufferSize(BUFFER_SIZE);
        policy.setMaxBinaryMessageSize(MAX_TEXT_MESSAGE_SIZE);
        policy.setInputBufferSize( BUFFER_SIZE);
        policy.setIdleTimeout(600000);

        return new DefaultHandshakeHandler(
                new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
    }
}

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketBrokerConfig extends WebSocketMessageBrokerConfigurationSupport implements WebSocketMessageBrokerConfigurer {


    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
        // Increase buffers. 
        // Too little buffers may result in fatal errros when transmitting relatively large messages.
        registry.setMessageSizeLimit(WebSocketTransportConfig.MAX_TEXT_MESSAGE_SIZE);
        registry.setSendBufferSizeLimit(WebSocketTransportConfig.BUFFER_SIZE);
        super.configureWebSocketTransport(registry);
    }
}

根据 Stomp spring web socket消息超出大小限制应该有帮助,但是在连接Java Spring SockJS客户端时,我仍然会收到错误1009(消息太大).

According to Stomp spring web socket message exceeds size limit that should help but when connecting Java Spring SockJS client, I still get error 1009 (too big message).

我怀疑因此,我可能会遇到以下两个问题之一:

I suspect therefore I may have one of two issues:

  1. 还必须将Java SockJS客户端配置为接收较大的消息.
  2. 或者,我仍然在服务器上配置错误.

如何在Java SockJS Spring客户端上增加缓冲区大小?

How can I increase buffer size on Java SockJS Spring client?

推荐答案

我偶然发现了同样的问题.这完全是关于客户端而不是服务器的配置. 您可以创建 WebSocketContainer 实例并对其进行配置.

I stumbled with the same problem. It's all about the configuration of the client, not the server. You can create WebSocketContainer instance and configure it.

WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.setDefaultMaxBinaryMessageBufferSize(your_size);
container.setDefaultMaxTextMessageBufferSize(your_size);
WebSocketClient transport = new StandardWebSocketClient(container);
WebSocketStompClient stompClient = new WebSocketStompClient(transport);

your_size-大小(以字节为单位).

your_size - size in bytes.

这篇关于Java SockJS Spring客户端和消息大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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