code=1009, reason=The decoded text message was too big for the output buffer and the endpoint does not support partial messages
tomcat默认实现文本缓冲区大小未8k,当接收内容超过这个容量,且没开启partial接收功能, 那么就关闭连接了。
1 2 3 4 5 |
WebSocketContainer container = ContainerProvider.getWebSocketContainer(); //container.setDefaultMaxBinaryMessageBufferSize(your_size); container.setDefaultMaxTextMessageBufferSize(10*1024); WebSocketClient client = new StandardWebSocketClient(container); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class RawWsSessionHandler extends AbstractWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message){ log.info(new Date() + "handleTextMessage: " + message.getPayload()); log.info("isLast:" + message.isLast()); } //打印连接关闭,能节省调试时间 @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status){ log.info("status:{}, msg:{}", status.getCode(), status.getReason()); } //实现支持部分消息 @Override public boolean supportsPartialMessages() { return true; } } |
发送
1 2 3 4 5 6 7 |
ByteBuffer buffer = ByteBuffer.allocate(100); boolean isLast = i + 100 >= payload.length; buffer.put(payload, i, isLast ? payload.length - i : 100); buffer.flip(); BinaryMessage message = new BinaryMessage(buffer, isLast); session.sendMessage(message); |
接收
1 2 3 4 5 6 7 8 9 10 11 |
public class RawWsSessionHandler implements WebSocketHandler { @Override protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) { super.handleBinaryMessage(session, message); ByteBuffer buffer = message.getPayload(); byte[] dst = new byte[buffer.remaining()]; buffer.get(dst); } } |
Posted in: java基础
Comments are closed.