繁体   English   中英

从Spring Websocket程序发送STOMP错误

[英]Send STOMP ERROR from Spring Websocket program

我有一个Spring Websocket Stomp应用程序,它接受SUBSCRIBE请求。

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

 @Component
 public class SubscribeStompEventHandler implements ApplicationListener<SessionSubscribeEvent> {

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

我用来验证订阅。

我会检查onApplicationEvent中的某些内容并从此函数将STOMP ERROR消息发送回客户端。

我找到了这个配方如何使用Spring WebSocket向STOMP客户端发送ERROR消息? 但我需要了解如何获得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());       
}

我试过主题成为一些subsciption主题和/ queue / error主题。 但是我没有看到消息传播到客户端。

在客户端,我使用:

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

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

请告诉我如何发送正确的STOMP错误,例如通过获取Outboundchannel。

您可以像这样发送ERROR消息:

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。

UPDATE

STOMP ERROR总是关闭连接? 我得到了这个效果。 代码1002。

是的。 请参阅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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM