我在Spring中使用STOMP创建了一个websocket。当与javascript库一起使用时,端点就像一个魅力,但当我使用任何简单的websocket google chrome扩展(即Simple WebSocket Client,Smart Websocket Client,Web Socket Client)时,spring会抛出"不完整的STOMP帧内容消息。深入了解代码,我已经能够看到原因是我无法使用任何这些工具插入空字符/ u0000。我假设所有的java脚本框架都默认执行此操作。有人为此找到了解决方法,以便我可以使用任何带有Spring STOMP的websocket客户端吗?
在[当前]行308-320上,存在以下代码。此方法返回null,因为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;
}
}
答案 0 :(得分:1)
我遇到了完全相同的问题,我使用Web套接字客户端进行了测试。
为了能够在我的本地环境中手动测试STOMP,我已经配置了我的Spring上下文。这样我就不需要在客户端添加空字符。如果它不存在,它会自动添加。
为此,在类 AbstractWebSocketMessageBrokerConfigurer 中我添加了:
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registration) {
registration.addDecoratorFactory(new WebSocketHandlerDecoratorFactory() {
@Override
public WebSocketHandler decorate(WebSocketHandler webSocketHandler) {
return new EmaWebSocketHandlerDecorator(webSocketHandler);
}
});
}
当没有请求体时,装饰器会自动添加回车符(例如:connect命令)。
/**
* 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上。