如何为springsockjswebsocket服务器实现增加输出缓冲区

6ioyuze2  于 2021-07-14  发布在  Java
关注(0)|答案(3)|浏览(872)

我使用了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]

你知道怎么增加缓冲区的大小吗
我使用了以下工厂,因为springsockjs利用了tomcat容器(应用程序部署在tomcat中,我还进行了调试,以确认它确实使用了tomcat lib)

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

然后我的urlMap看起来

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

我需要把这个豆子放在什么地方吗?sockjs怎么知道它必须使用这个facory?

kmb7vmvb

kmb7vmvb1#

通过使用来自http://docs.spring.io/spring/docs/4.0.1.release/javadoc-api/index.html?org/springframework/web/socket/sockjs/sockjsservice.html -获得了servletservercontainerfactorybean并设置了属性,这就成功了

@Bean
public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
    ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
    container.setMaxTextMessageBufferSize(32768);
    container.setMaxBinaryMessageBufferSize(32768);
    logger.info("Websocket factory returned");
    return container;
}
egmofgnx

egmofgnx2#

对于客户端:

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

lrpiutwd3#

您可以配置websocket引擎并增加缓冲区大小。
注意,根据您想要使用的实际大小,记住这些消息将完全缓冲在内存中!如果您的客户机支持,您可能需要考虑使用部分消息。

相关问题