首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用向特定用户发送消息

使用向特定用户发送消息
EN

Stack Overflow用户
提问于 2020-01-20 16:12:34
回答 1查看 4.1K关注 0票数 3

我在本教程中设置了一个带有spring的WebSocket:https://spring.io/guides/gs/messaging-stomp-websocket/。我需要的是我的服务器每5秒向特定用户发送一条消息。所以我第一次做了这个:

代码语言:javascript
复制
@Autowired
private SimpMessagingTemplate template;

@Scheduled(fixedRate = 5000)
public void greet() {
    template.convertAndSend("/topic/greetings", new Greeting("Bufff!"));
}

而且它是有效的。现在,为了只向特定用户发送消息,我更改了以下内容:

代码语言:javascript
复制
@Scheduled(fixedRate = 5000)
public void greet() {
   template.convertAndSendToUser("MyName","/queue/greetings", new Greeting("Bufff!"));
}

在WebSocketConfig.java中添加队列:

代码语言:javascript
复制
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic","/queue");
    config.setApplicationDestinationPrefixes("/app");
}

更改GreetingController.java中的注释:

代码语言:javascript
复制
@MessageMapping("/hello")
@SendToUser("/queue/greetings")
public Greeting UserGreeting(HelloMessage message, Principal principal) throws Exception {
    Thread.sleep(1000); // simulated delay
    return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
}

并在app.js中更改连接函数:

代码语言:javascript
复制
var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
    setConnected(true);
    console.log('Connected: ' + frame);
    stompClient.subscribe('user/queue/greetings', function (greeting) {
        showGreeting(JSON.parse(greeting.body).content);
    });
});

服务器使用spring引导安全性,如果MyName是正确的名称,我使用SimpUserRegistry https://stackoverflow.com/a/32215398/11663023查找所有用户)。但不幸的是,我的代码不起作用。我已经尝试过这个Sending message to specific user using spring,但是我不希望Spring区分会话而是用户。我也看过这个Sending message to specific user on Spring Websocket,但是它没有帮助,因为链接不起作用。

那是我的控制台日志:

代码语言:javascript
复制
2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Searching methods to handle SEND /app/hello session=kvv0m1qm, lookupDestination='/hello'
2020-01-20 17:08:51.352 DEBUG 8736 --- [nboundChannel-3] .WebSocketAnnotationMethodMessageHandler : Invoking de.iteratec.iteraweb.controllers.GreetingController#UserGreeting[2 args]
2020-01-20 17:08:52.354 DEBUG 8736 --- [nboundChannel-3] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Hello, hey!"}
2020-01-20 17:08:54.882 DEBUG 8736 --- [MessageBroker-2] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}
2020-01-20 17:08:59.883 DEBUG 8736 --- [MessageBroker-4] org.springframework.web.SimpLogging      : Processing MESSAGE destination=/queue/greetings-userkvv0m1qm session=null payload={"content":"Bufff!"}

我错过了改变吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-26 05:33:40

您能看到客户端成功地订阅了您的端点吗?

我认为您缺少了客户机代码中的第一个/'user/queue/greetings'应该是'/user/queue/greetings'

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59827107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档