13

我使用了 sockjs websocket 服务器的 spring 实现,无法传输超过 8KB 的消息,以下是错误

2014-02-12 19:36:29,990 - org.springframework.web.socket.sockjs.transport.session.WebSocketServerSockJsSession - DEBUG - SockJS session id=35n41xel was closed, CloseStatus [code=1009, reason=The decoded text message was too big for the output buffer and the endpoint does not support partial messages]

任何想法如何增加缓冲区大小


我使用了以下工厂,因为 spring sockjs 利用了 tomcat 容器(应用程序部署在 tomcat 中,我还进行了调试以确认它确实使用了 tomcat lib)

@Bean
public WebSocketContainerFactoryBean createWebSocketContainer() {
    WebSocketContainerFactoryBean container = new WebSocketContainerFactoryBean();
    container.setMaxTextMessageBufferSize(16384);
    container.setMaxBinaryMessageBufferSize(8192);
    return container;
}

然后我的 URL 映射看起来

@Override 
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(coBrowseSockJsCustomerHandler(), "/sockjs/cobrowse/customer/").withSockJS();}

我需要在某个地方用 sockjs 设置这个 bean 吗?sockjs 怎么知道它必须使用这个工厂?

4

4 回答 4

16

通过使用来自http://docs.spring.io/spring/docs/4.0.1.RELEASE/javadoc-api/index.html?org/springframework/web/socket/sockjs/SockJsService.html的线索解决了它-got hold ServletServerContainerFactoryBean 并设置属性,这行得通

@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
    ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
    container.setMaxTextMessageBufferSize(32768);
    container.setMaxBinaryMessageBufferSize(32768);
    logger.info("Websocket factory returned");
    return container;
}
于 2014-02-17T07:15:13.787 回答
11

对于客户端:

@Bean
public static WebSocketStompClient getClient() {
    List<Transport> transports = new ArrayList<>();
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    container.setDefaultMaxBinaryMessageBufferSize(1024 * 1024);
    container.setDefaultMaxTextMessageBufferSize(1024 * 1024);
    transports.add(new WebSocketTransport(new StandardWebSocketClient(container)));
    WebSocketClient webSocketClient = new SockJsClient(transports);
    WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
    stompClient.setInboundMessageSizeLimit(Integer.MAX_VALUE);
    stompClient.setMessageConverter(new MappingJackson2MessageConverter());
    return stompClient;
}

对于服务器端:

@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
    ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
    container.setMaxTextMessageBufferSize(32768);
    container.setMaxBinaryMessageBufferSize(32768);
    logger.info("Websocket factory returned");
    return container;
}
于 2017-07-10T16:23:29.590 回答
1

您可以配置 websocket 引擎并增加缓冲区大小。

请注意,根据您要使用的实际大小,请记住这些消息将完全缓冲在内存中!如果您的客户支持,您可能需要考虑使用部分消息。

于 2014-02-16T09:04:17.597 回答
0

在我的 web.xml 文件中设置它对我有用:

<context-param>
  <param-name>org.apache.tomcat.websocket.textBufferSize</param-name>
  <param-value>65536</param-value>
</context-param>
<context-param>
  <param-name>org.apache.tomcat.websocket.binaryBufferSize</param-name>
  <param-value>65536</param-value>
</context-param>
于 2021-06-24T17:56:54.637 回答