![](/img/trans.png)
[英]How to send ERROR message to STOMP clients with Spring WebSocket?
[英]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.