Spring STOMP 不完整的帧

Spring STOMP Incomplete Frame

我在 Spring 中使用 STOMP 创建了一个 websocket。当与 java 脚本库一起使用时,端点就像一个魅力但是当我使用任何简单的 websocket google chrome 扩展(即简单的 WebSocket 客户端、智能 Websocket 客户端、Web 套接字客户端)时, spring 抛出“不完整的 STOMP 帧内容消息。深入研究代码,我已经能够看到这是我无法使用这些工具中的任何一个插入空字符 /u0000 的原因。我假设所有java 脚本框架默认执行此操作。是否有人为此找到了解决方法,以便我可以将任何 websocket 客户端与 Spring STOMP 一起使用?

踩踏代码位于此处:https://github.com/spring-projects/spring-framework/blob/master/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompDecoder.java

[当前] 第 308-320 行存在以下代码。此方法 returns 为空,因为 byteBuffer.remaining 不大于内容长度(均为 0)。之后会触发 StompSubProtocolHandler 异常。我试着查看所有的处理程序和拦截器,但似乎没有办法在不重写几乎所有内容的情况下拦截这个级别的东西。我只想将“\0”注入有效负载...

if (contentLength != null && contentLength >= 0) {
        if (byteBuffer.remaining() > contentLength) {
            byte[] payload = new byte[contentLength];
            byteBuffer.get(payload);
            if (byteBuffer.get() != 0) {
                throw new StompConversionException("Frame must be terminated with a null octet");
            }
            return payload;
        }
        else {
            return null;
        }
    }

我遇到了完全相同的问题,我使用 Web 套接字客户端进行了测试。

为了能够在本地环境中手动测试 STOMP,我配置了 Spring 上下文。这样我就不需要在客户端添加空字符。不存在自动添加

为此,在 class AbstractWebSocketMessageBrokerConfigurer 我添加了:

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
        @Override
        public WebSocketHandler decorate(WebSocketHandler webSocketHandler) {
            return new EmaWebSocketHandlerDecorator(webSocketHandler);
        }
    });
}

装饰器在没有请求主体(例如:连接命令)时自动添加回车returns。

/**
 * Extension of the {@link WebSocketHandlerDecorator websocket handler decorator} that allows to manually test the
 * STOMP protocol.
 *
 * @author Sebastien Gerard
 */
public class EmaWebSocketHandlerDecorator extends WebSocketHandlerDecorator {

    private static final Logger logger = LoggerFactory.getLogger(EmaWebSocketHandlerDecorator.class);

    public EmaWebSocketHandlerDecorator(WebSocketHandler webSocketHandler) {
        super(webSocketHandler);
    }

    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
        super.handleMessage(session, updateBodyIfNeeded(message));
    }

    /**
     * Updates the content of the specified message. The message is updated only if it is
     * a {@link TextMessage text message} and if does not contain the <tt>null</tt> character at the end. If
     * carriage returns are missing (when the command does not need a body) there are also added.
     */
    private WebSocketMessage<?> updateBodyIfNeeded(WebSocketMessage<?> message) {
        if (!(message instanceof TextMessage) || ((TextMessage) message).getPayload().endsWith("\u0000")) {
            return message;
        }

        String payload = ((TextMessage) message).getPayload();

        final Optional<StompCommand> stompCommand = getStompCommand(payload);

        if (!stompCommand.isPresent()) {
            return message;
        }

        if (!stompCommand.get().isBodyAllowed() && !payload.endsWith("\n\n")) {
            if (payload.endsWith("\n")) {
                payload += "\n";
            } else {
                payload += "\n\n";
            }
        }

        payload += "\u0000";

        return new TextMessage(payload);
    }

    /**
     * Returns the {@link StompCommand STOMP command} associated to the specified payload.
     */
    private Optional<StompCommand> getStompCommand(String payload) {
        final int firstCarriageReturn = payload.indexOf('\n');

        if (firstCarriageReturn < 0) {
            return Optional.empty();
        }

        try {
            return Optional.of(
                    StompCommand.valueOf(payload.substring(0, firstCarriageReturn))
            );
        } catch (IllegalArgumentException e) {
            logger.trace("Error while parsing STOMP command.", e);

            return Optional.empty();
        }
    }
}

现在我可以执行以下请求:

CONNECT
accept-version:1.2
host:localhost
content-length:0


SEND
destination:/queue/com.X.notification-subscription
content-type:text/plain
reply-to:/temp-queue/notification

hello world :)

希望对您有所帮助。

S.