带有用户目标的Spring Stomp @SubscribeMapping("/user/...")不起作用

编程入门 行业动态 更新时间:2024-10-27 06:23:44
本文介绍了带有用户目标的Spring Stomp @SubscribeMapping("/user/...")不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要对用户目标订阅作出反应.

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/...")不起作用

本文发布于:2023-11-24 11:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624965.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   目标   用户   user   Stomp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!