【发布时间】:2016-02-21 17:17:59
【问题描述】:
我正在尝试使用 STOMP over SockJS 在 Spring websockets 中发送错误消息。
我基本上是在尝试实现here。
这是我的异常处理程序
@MessageExceptionHandler
@SendToUser(value = "/queue/error",broadcast = false)
public ApplicationError handleException(Exception message) throws ApplicationError {
return new ApplicationError("test");
}
我订阅了
stompClient.subscribe('/user/queue/error', stompErrorCallback, {token: accessToken});
在我的情况下,用户未经过身份验证,但来自 here
虽然用户目的地通常意味着经过身份验证的用户,但它 不是严格要求的。未关联的 WebSocket 会话 使用经过身份验证的用户可以订阅用户目的地。在 在这种情况下,@SendToUser 注释的行为将与 使用广播=false,即仅针对发送 正在处理的消息。
当我从myHandler(这是我在 websocket 配置中定义的 Websocket 处理程序)抛出此错误时,所有这些都可以正常工作。
我有一个ClientInboundChannelInterceptor,它扩展了ChannelInterceptorAdapter,它拦截了preSend 中的所有消息。
如果这个拦截器出现任何异常,我想把它扔回发送这个消息的用户会话,
public class ClientInboundChannelInterceptor extends ChannelInterceptorAdapter {
@Autowired
@Lazy(value = true)
@Qualifier("brokerMessagingTemplate")
private SimpMessagingTemplate simpMessagingTemplate;
@Override
public Message<?> preSend(Message message, MessageChannel channel) throws IllegalArgumentException{
if(some thing goes wrong)
throw new RuntimeException();
}
@MessageExceptionHandler
@SendToUser(value = "/queue/error",broadcast = false)
public ApplicationError handleException(RuntimeException message) throws ApplicationError {
return new ApplicationError("test");
}
}
@MessageExceptionHandler 没有捕捉到这个异常。所以我尝试使用simpMessagingTemplate 直接将其发送给用户。
我基本上是想做的:
simpMessagingTemplate.convertAndSendToUser(SOMETHING,"/queue/error",e);
SOMETHING 应该是正确的用户名,但在我的情况下用户没有经过身份验证,所以我不能使用 headerAccessor.getUser().getName()
我什至尝试过
simpMessagingTemplate.convertAndSendToUser(headerAccessor.getHeader("","/queue/error",e, Collections.singletonMap(SimpMessageHeaderAccessor.SESSION_ID_HEADER, headerAccessor.getSessionId()));
但这不起作用。
我什至尝试用headerAccessor.getSessionId() 代替用户名,但这似乎不起作用。
这样做的正确方法是什么?
convertAndSendToUser 中的用户名应该使用什么?
【问题讨论】:
标签: spring stomp spring-websocket spring-messaging