[英]Send STOMP ERROR from Spring Websocket program
I have a Spring Websocket Stomp application that accepts SUBSCRIBE requests. 我有一个Spring Websocket Stomp应用程序,它接受SUBSCRIBE请求。
In application I have a handler for SUBSCRIBE, that is, 在应用程序中,我有一个SUBSCRIBE的处理程序,即
@Component
public class SubscribeStompEventHandler implements ApplicationListener<SessionSubscribeEvent> {
@Override
public void onApplicationEvent(SessionSubscribeEvent event) {}
}
that I use to validate subscription. 我用来验证订阅。
I would check something in the onApplicationEvent and send STOMP ERROR message back to client from this function. 我会检查onApplicationEvent中的某些内容并从此函数将STOMP ERROR消息发送回客户端。
I found this recipe How to send ERROR message to STOMP clients with Spring WebSocket? 我找到了这个配方如何使用Spring WebSocket向STOMP客户端发送ERROR消息? but I need to understand how to get outboundChannel.
但我需要了解如何获得outboundChannel。
I tried also the following code: 我还尝试了以下代码:
public void sendStompError(SimpMessagingTemplate simpMessagingTemplate, String sessionId, String topic, String errorMessage) {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
headerAccessor.setMessage(errorMessage);
headerAccessor.setSessionId(sessionId);
headerAccessor.setLeaveMutable(true);
simpMessagingTemplate.convertAndSendToUser(sessionId, topic, new byte[0], headerAccessor.getMessageHeaders());
}
and I tried topic to be some subsciption topic and /queue/error topic. 我试过主题成为一些subsciption主题和/ queue / error主题。 However I did not see messages propagating to client.
但是我没有看到消息传播到客户端。
In Client, I use: 在客户端,我使用:
stompClient.connect(headers
, function (frame) {
console.log("Conn OK " + url);
}, function (error) {
console.log("Conn NOT OK " + url + ": " + JSON.stringify(error));
});
}
and my goal is to have function(error) called when I send STOMP ERROR. 我的目标是在发送STOMP ERROR时调用函数(错误)。
Please advice me how exactly I can send proper STOMP ERROR, eg by getting Outboundchannel. 请告诉我如何发送正确的STOMP错误,例如通过获取Outboundchannel。
You can send ERROR
Message like this: 您可以像这样发送
ERROR
消息:
StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
headerAccessor.setMessage(error.getMessage());
headerAccessor.setSessionId(sessionId);
this.clientOutboundChannel.send(MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders()));
The following is just enough to inject that clientOutboundChannel
: 以下就足以注入
clientOutboundChannel
:
@Autowired
@Qualifier("clientOutboundChannel")
private MessageChannel clientOutboundChannel;
Just because clientOutboundChannel
bean is declared in the AbstractMessageBrokerConfiguration
. 仅仅因为在
AbstractMessageBrokerConfiguration
声明了clientOutboundChannel
bean。
UPDATE UPDATE
STOMP ERROR always closes connection?
STOMP ERROR总是关闭连接? I am getting this effect.
我得到了这个效果。 Code 1002.
代码1002。
Yes, it is. 是的。 See
StompSubProtocolHandler.sendToClient()
: 请参阅
StompSubProtocolHandler.sendToClient()
:
if (StompCommand.ERROR.equals(command)) {
try {
session.close(CloseStatus.PROTOCOL_ERROR);
}
catch (IOException ex) {
// Ignore
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.