如何增加spring sockjs websocket服务器实现的输出缓冲区

时间:2014-02-12 14:20:38

标签: spring websocket sockjs spring-websocket

我使用了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容器(App部署在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 个答案:

答案 0 :(得分:9)

使用来自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;
}

答案 1 :(得分:4)

客户端

@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;
}

答案 2 :(得分:1)

您可以configure the websocket engine并增加缓冲区大小。

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

答案 3 :(得分: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>