通过WebSocket发送Stomp消息:发送缓冲区大小超过允许的限制。

10

在客户端,我使用Stomp进行WebSocket连接,在服务器端,我使用Spring 4。 在客户端,我进行了如下配置:

var socket = new SockJS(urlBase + "/" + contextroot+'/hello');
stompClient = Stomp.over(socket);

以下代码每2秒执行一次,用于将数据发送到服务器。

stompClient.send('/app/sendRequest/'+indexVal, {}, 
JSON.stringify({index : simIndex}));

服务器响应以下队列

stompClient.subscribe('/queue/response', processResponseObj);

服务器端配置完成如下:

<websocket:message-broker application-destination-prefix="/app">
   <!--<websocket:transport send-timeout="15000" message-size="1051648" send-buffer-size="1051648"/> -->
   <websocket:stomp-endpoint path="/hello">
       <websocket:sockjs />
   </websocket:stomp-endpoint>
   <websocket:stomp-broker-relay prefix="/topic, /queue"  />

    <websocket:message-converters>
        <beans:bean class="org.springframework.messaging.converter.MappingJackson2MessageConverter">
        <beans:property name="objectMapper" ref="objectMapper" />
       </beans:bean>
    </websocket:message-converters>

    </websocket:message-broker>

服务器已响应消息

 messagingTemplate.convertAndSend("/queue/response",obj);

几秒后我收到了错误信息

2014-10-24 16:39:33,869 ERROR et.messaging.SubProtocolWebSocketHandler: 330 - Terminating session id 'dkbzrkxp'
org.springframework.web.socket.handler.SessionLimitExceededException: The send buffer size 1147188 bytes for session 'dkbzrkxp exceeded the allowed limit 1051648
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sessionLimitReached(ConcurrentWebSocketSessionDecorator.java:162) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.checkSessionLimits(ConcurrentWebSocketSessionDecorator.java:150) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sendMessage(ConcurrentWebSocketSessionDecorator.java:105) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageToClient(StompSubProtocolHandler.java:276) ~[StompSubProtocolHandler.class:4.0.6.RELEASE]
    at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:326) ~[SubProtocolWebSocketHandler.class:4.0.6.RELEASE]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$1.run(ExecutorSubscribableChannel.java:70) [ExecutorSubscribableChannel$1.class:4.0.6.RELEASE]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_20]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_20]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_20]
2014-10-24 16:39:33,871  WARN ort.session.WebSocketServerSockJsSession: 285 - Failed to send SockJS close frame: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method
2014-10-24 16:39:33,875 ERROR et.messaging.SubProtocolWebSocketHandler: 330 - Terminating session id 'nro1ww4x'
org.springframework.web.socket.handler.SessionLimitExceededException: The send buffer size 1147188 bytes for session 'nro1ww4x exceeded the allowed limit 1051648
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sessionLimitReached(ConcurrentWebSocketSessionDecorator.java:162) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.checkSessionLimits(ConcurrentWebSocketSessionDecorator.java:150) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator.sendMessage(ConcurrentWebSocketSessionDecorator.java:105) ~[ConcurrentWebSocketSessionDecorator.class:4.0.6.RELEASE]
    at org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageToClient(StompSubProtocolHandler.java:276) ~[StompSubProtocolHandler.class:4.0.6.RELEASE]
    at org.springframework.web.socket.messaging.SubProtocolWebSocketHandler.handleMessage(SubProtocolWebSocketHandler.java:326) ~[SubProtocolWebSocketHandler.class:4.0.6.RELEASE]
    at org.springframework.messaging.support.ExecutorSubscribableChannel$1.run(ExecutorSubscribableChannel.java:70) [ExecutorSubscribableChannel$1.class:4.0.6.RELEASE]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_20]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_20]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_20]
2014-10-24 16:39:33,876  WARN ort.session.WebSocketServerSockJsSession: 285 - Failed to send SockJS close frame: The remote endpoint was in state [TEXT_PARTIAL_WRITING] which is an invalid state for called method
2014-10-24 16:39:34,833 ERROR standard.StandardWebSocketHandlerAdapter:  55 - Closing due to exception for WebSocket session id=0

我尝试增加缓冲区大小,但是在一段时间后它会产生相同的异常,一旦缓冲区大小到达。

我认为WebSocket会话缓冲区在消息传递后没有被释放。

是否需要设置相同的内容? 我是否错过了任何配置?


你解决了这个问题吗?我也遇到了同样的问题。 - Brian Sternari
通过添加 registration.setMessageSizeLimit(102400* 1024); 解决。 - Deepak Mule
1个回答

13

尝试在服务器端配置Web套接字。类似于WebSocketConfig类中的以下内容:

 public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.setMessageSizeLimit(500 * 1024);
    registration.setSendBufferSizeLimit(1024 * 1024);
    registration.setSendTimeLimit(20000);
}

1
除了@skywalker的回答之外,我还找到了这个Spring文档页面供您参考(例如消息大小限制、缓冲区大小限制等的默认值) https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/socket/config/annotation/WebSocketTransportRegistration.html - Minjeong Choi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接