从Spring Websocket程序发送STOMP错误

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

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

@Component public class SubscribeStompEventHandler implements ApplicationListener { @Override public void onApplicationEvent(SessionSubscribeEvent event) {} } 

我用来validation订阅。

我会检查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 } }