Send STOMP ERROR from Spring Websocket program

隐身守侯 提交于 2019-12-04 15:26:59

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

The following is just enough to inject that clientOutboundChannel:

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

Just because clientOutboundChannel bean is declared in the AbstractMessageBrokerConfiguration.

UPDATE

STOMP ERROR always closes connection? I am getting this effect. Code 1002.

Yes, it is. See StompSubProtocolHandler.sendToClient():

       if (StompCommand.ERROR.equals(command)) {
            try {
                session.close(CloseStatus.PROTOCOL_ERROR);
            }
            catch (IOException ex) {
                // Ignore
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!