我需要对用户目标订阅作出反应.
I need to react on a user destination subscription.
用户订阅/user/messages,因为他想接收所有传入的消息.现在,我想查找该用户离线时创建的该用户的所有消息,然后将其发送给该用户.
A user subscribes to /user/messages, because he wants to receive all incoming messages. Now I'd like to look up any messages for this user, which were created while he was offline, and then send them to that user.
客户代码:
stompClient.subscribe('/user/messages', function(msg){ alert(msg.body); });服务器代码:
template.convertAndSendToUser(p.getName(), "/messages", "message content");我需要什么:
似乎无法在服务器端捕获用户目标订阅,即:
What I need:
It seems like it's not possible to catch an user destination subscription on server side, i.e.:
@SubscribeMapping("/user/messages") public void test(Principal p) { sendMessagesThatWereReceivedWhileUserWasOffline(); }我尝试过的事情:
@SubscribeMapping("/messages") public void test(Principal p) { ... }这有效,如果客户端订阅了/app/messages,,但不会被呼叫 /user/messages.
This works, if the client subscribes to /app/messages, but it won't get called for /user/messages.
@Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/stomp").withSockJS(); } @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/app"); registry.enableSimpleBroker("/queue", "/topic"); registry.setUserDestinationPrefix("/user"); } @Override public boolean configureMessageConverters(List<MessageConverter> messageConverters) { return true; } // all other methods left empty }使用Spring 4.1.
Using Spring 4.1.
我无法想象这是不可能的.我错过/做错了什么?
I can't imagine that this isn't possible. What have I missed / done wrong?
谢谢您:)
推荐答案还将用户前缀定义为应用程序前缀,然后您就可以在控制器中映射订阅.配置:
Define the user prefix also as an application prefix, and you'll then be able to map the subscription in your controller. Configuration:
@Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.setApplicationDestinationPrefixes("/app", "/user"); registry.enableSimpleBroker("/queue", "/topic"); registry.setUserDestinationPrefix("/user"); }控制器:
@SubscribeMapping("/messages") public void test(Principal p) { sendMessagesThatWereReceivedWhileUserWasOffline(); }更多推荐
带有用户目标的Spring Stomp @SubscribeMapping("/user/...")不起作用
发布评论