How to increase output buffer for spring sockjs websocket server implementation

后端 未结 3 1131
孤城傲影
孤城傲影 2020-12-17 18:47

I have used spring implementation of sockjs websocket server and unable to transmit message over 8KB, following is the error

2014-02-12 19:36:29,990 - org.sp         


        
相关标签:
3条回答
  • 2020-12-17 19:03

    You can configure the websocket engine and increase the buffer size.

    Watch out, depending on the actual size you'd like to use, remember that those messages will be buffered entirely in memory! You may want to consider using partial messages if your client supports it.

    0 讨论(0)
  • 2020-12-17 19:04

    for client side:

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

    for server side:

    @Bean
    public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(32768);
        container.setMaxBinaryMessageBufferSize(32768);
        logger.info("Websocket factory returned");
        return container;
    }
    
    0 讨论(0)
  • 2020-12-17 19:26

    Solved it by using clue from http://docs.spring.io/spring/docs/4.0.1.RELEASE/javadoc-api/index.html?org/springframework/web/socket/sockjs/SockJsService.html -got hold of ServletServerContainerFactoryBean and set the properties, this worked

    @Bean
    public ServletServerContainerFactoryBean createServletServerContainerFactoryBean() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(32768);
        container.setMaxBinaryMessageBufferSize(32768);
        logger.info("Websocket factory returned");
        return container;
    }
    
    0 讨论(0)
提交回复
热议问题