本文介绍了从Spring Websocket程序发送STOMP ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring Websocket Stomp应用程序,可以接受SUBSCRIBE请求.

I have a Spring Websocket Stomp application that accepts SUBSCRIBE requests.

在应用程序中,我有一个SUBSCRIBE的处理程序,即

In application I have a handler for SUBSCRIBE, that is,

 @Component
 public class SubscribeStompEventHandler implements ApplicationListener<SessionSubscribeEvent> {

    @Override
    public void onApplicationEvent(SessionSubscribeEvent event) {}
 }

我用来验证订阅的

.

that I use to validate subscription.

我会检查 onApplicationEvent 中的内容,然后通过此功能将STOMP ERROR消息发送回客户端.

I would check something in the onApplicationEvent and send STOMP ERROR message back to client from this function.

我找到了这个食谱如何发送使用Spring WebSocket向STOMP客户端发送错误消息?,但我需要了解如何获取outboundChannel.

I found this recipe How to send ERROR message to STOMP clients with Spring WebSocket? but I need to understand how to get outboundChannel.

我也尝试了以下代码:

   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());
}

,并且我尝试将主题设置为一些订阅主题和/queue/error主题.但是,我没有看到传播到客户端的消息.

and I tried topic to be some subsciption topic and /queue/error topic. However I did not see messages propagating to client.

在客户端中,我使用:

      stompClient.connect(headers
                , function (frame) {
                    console.log("Conn OK " + url);
                }, function (error) {
                    console.log("Conn NOT OK " + url + ": " + JSON.stringify(error));
                });
        }

,我的目标是在发送STOMP ERROR时调用函数(错误).

and my goal is to have function(error) called when I send STOMP ERROR.

请告知我如何正确发送STOMP错误,例如通过获取Outboundchannel.

Please advice me how exactly I can send proper STOMP ERROR, e.g. by getting Outboundchannel.

推荐答案

您可以发送ERROR消息,如下所示:

You can send ERROR Message like this:

StompHeaderAccessor headerAccessor = StompHeaderAccessor.create(StompCommand.ERROR);
headerAccessor.setMessage(error.getMessage());
headerAccessor.setSessionId(sessionId);
this.clientOutboundChannel.send(MessageBuilder.createMessage(new byte[0], headerAccessor.getMessageHeaders()));

以下内容足以注入clientOutboundChannel:

 @Autowired
 @Qualifier("clientOutboundChannel")
 private MessageChannel clientOutboundChannel;

只是因为AbstractMessageBrokerConfiguration中声明了clientOutboundChannel bean.

Just because clientOutboundChannel bean is declared in the AbstractMessageBrokerConfiguration.

更新

是的.参见StompSubProtocolHandler.sendToClient():

       if (StompCommand.ERROR.equals(command)) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            }
            catch (IOException ex) {
                // Ignore
            }
        }

这篇关于从Spring Websocket程序发送STOMP ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 13:44