【问题标题】:Spring Stomp over Websocket: Message/Buffer/Cache/Stream limitsSpring Stomp over Websocket:消息/缓冲区/缓存/流限制
【发布时间】:2016-06-18 10:05:13
【问题描述】:

无法理解我用于开发涉及图像/视频的聊天应用程序的 stomp over websocket 配置中的不同参数:

我注意到网页中的 SockJs 发送帧大小为 16K 的消息。我还测试了消息大小限制决定了我可以传输的最大消息大小。

请告诉我是什么:

  1. 流字节数限制

  2. 发送缓冲区大小限制

  3. http 消息缓存大小

  4. 什么是部分消息以及如何使用它们,它们在这里有用吗?

  5. 我还计划将图像/视频的最大大小设置为 2GB,并预计在我发布时同时有大约 100 个用户。

您能否告诉我们我应该保留什么尺寸以及为什么?什么是默认值?以及它们各自会如何影响我的聊天应用程序的性能?

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/stomp").withSockJS()
            .setStreamBytesLimit(15 * 1024)
            .setHttpMessageCacheSize(15 * 1024);
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/queue/", "/topic/", "/exchange/");
    registry.setApplicationDestinationPrefixes("/app");
}

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.setSendTimeLimit(15 * 1000)
            .setSendBufferSizeLimit(1 * 1024)
            // max message size 2GB (2048 bytes) : default is 64KB
            .setMessageSizeLimit(2 * 1024 * 1024);
}

}

【问题讨论】:

    标签: java spring stomp spring-websocket sockjs


    【解决方案1】:

    用我的发现和实施回答问题:

    在以下配置中:

    @Override
    public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
        registration.setSendTimeLimit(60 * 1000)
                .setSendBufferSizeLimit(200 * 1024 * 1024)
                .setMessageSizeLimit(200 * 1024 * 1024);
    }
    
    1. 流字节限制:来自源的信息

      /**
       * Streaming transports save responses on the client side and don't free
       * memory used by delivered messages. Such transports need to recycle the
       * connection once in a while. This property sets a minimum number of bytes
       * that can be send over a single HTTP streaming request before it will be
       * closed. After that client will open a new request. Setting this value to
       * one effectively disables streaming and will make streaming transports to
       * behave like polling transports.
       * <p>The default value is 128K (i.e. 128 * 1024).
       */
      public SockJsServiceRegistration setStreamBytesLimit(int streamBytesLimit) {
          this.streamBytesLimit = streamBytesLimit;
          return this;
      }
      
    2. 发送缓冲区大小限制 默认为 512KB。如果消息发送速度较慢,则缓冲后续消息,直到达到 sendTimeLimit 或 sendBufferSizeLimit。

    3. http 消息缓存大小:来自源的信息

      /**
       * The number of server-to-client messages that a session can cache while waiting for
       * the next HTTP polling request from the client. All HTTP transports use this
       * property since even streaming transports recycle HTTP requests periodically.
       * <p>The amount of time between HTTP requests should be relatively brief and will not
       * exceed the allows disconnect delay (see
       * {@link #setDisconnectDelay(long)}), 5 seconds by default.
       * <p>The default size is 100.
       */
      public SockJsServiceRegistration setHttpMessageCacheSize(int httpMessageCacheSize) {
          this.httpMessageCacheSize = httpMessageCacheSize;
          return this;
      }
      
    4. 什么是部分消息以及如何使用它们,它们在这里有用吗? 仍然不确定如何通过 websocket 流式传输大文件并使用部分消息传递(决定使用 HTTP 代替)

    5. 我还计划将图像/视频的最大大小设置为 2GB,并在我发布时预计同时有大约 100 个用户。 => 由 messageSizeLimit 设置 并使用 HTTP 进行文件上传/流式下载。还可以使用 apache file-upload config 设置服务器限制:

      //set up the server limits using apache file-upload config
      @Bean
      public CommonsMultipartResolver multipartResolver() {
          CommonsMultipartResolver resolver = new CommonsMultipartResolver();
          resolver.setMaxUploadSize(2 * 1024 * 1024 * 1024); // 2 GB limit set for file upload
          resolver.setDefaultEncoding("utf-8");
          return resolver;
          }
      

    【讨论】:

    • 如何配置SockJsServiceRegistration?(能否提供代码如何获取以及如何配置?只是新的SockJsServiceRegistration?)