Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 弹簧踩踏不完全框架_Spring_Spring Boot_Stomp_Spring Framework Beans - Fatal编程技术网

Spring 弹簧踩踏不完全框架

Spring 弹簧踩踏不完全框架,spring,spring-boot,stomp,spring-framework-beans,Spring,Spring Boot,Stomp,Spring Framework Beans,我已经创建了一个websocket,使用STOMP inside Spring。当与javascript库一起使用时,端点就像一个魅力,然而当我使用任何简单websocket google chrome扩展(即简单websocket客户端、智能websocket客户端、Web套接字客户端)时,spring抛出“STOMP帧内容消息不完整。深入到代码中,我发现导致这种情况的原因是我无法使用这些工具中的任何一个插入空字符/u0000。我假设默认情况下所有java脚本框架都会这样做。是否有人找到了解决

我已经创建了一个websocket,使用STOMP inside Spring。当与javascript库一起使用时,端点就像一个魅力,然而当我使用任何简单websocket google chrome扩展(即简单websocket客户端、智能websocket客户端、Web套接字客户端)时,spring抛出“STOMP帧内容消息不完整。深入到代码中,我发现导致这种情况的原因是我无法使用这些工具中的任何一个插入空字符/u0000。我假设默认情况下所有java脚本框架都会这样做。是否有人找到了解决方法,以便我可以使用任何带有Spring STOMP的websocket客户端

stomp代码位于此处:

在[当前]第308-320行,存在以下代码。此方法返回null,因为byteBuffer.remaining不大于内容长度(两者均为0)。存在引发afterrwards的StompSubtocolHandler异常。我尝试查看所有的处理程序和拦截器,但似乎没有一种方法可以在不重写几乎所有内容的情况下在这个级别截取内容。我只想将“\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上下文。这样我就不需要在客户端添加空字符。如果它不存在,则会自动添加

为此,我在AbstractWebSocketMessageBrokerConfigure类中添加了:

当没有请求主体(例如:connect命令)时,decorator add自动返回

希望这有帮助


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

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

为此,我在AbstractWebSocketMessageBrokerConfigure类中添加了:

当没有请求主体(例如:connect命令)时,decorator add自动返回

希望这有帮助

美国

@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
    registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
        @Override
        public WebSocketHandler decorate(WebSocketHandler webSocketHandler) {
            return new EmaWebSocketHandlerDecorator(webSocketHandler);
        }
    });
}
/**
 * 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 :)