gpt4 book ai didi

java - Spring框架Web套接字独特的@SendTo注释

转载 作者:行者123 更新时间:2023-12-02 02:33:46 25 4
gpt4 key购买 nike

我有一个已注册用户的网络应用程序,我想将消息推送给具有唯一用户 ID 的用户。为了能够向客户端发送消息,我需要知道如何在 @sendto 注释中传递唯一的用户 ID

这是注释

@SendTo("/topic/uniqueuserid")
public Greeting greeting(HelloMessage message) throws Exception {
int uniqueuserid;
Thread.sleep(1000); // simulated delay
return new Greeting("Hello, " + message.getName() + uniqueuserid "!");
}

这是 stomp js

stompClient.subscribe('/topic/uniqueuserid', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});

如何在 @SendTo("/topic/uniqueuserid") 中传递唯一的用户 ID

最佳答案

您可以在方法参数中使用@DestinationVariable注释,如下所示:

@MessageMapping("/mychat/{uniqueUserId}")
@SendTo("/topic/{uniqueUserId}")
public Message message(@DestinationVariable("uniqueUserId") Long uniqueUserId, HelloMessage message) {
logger.info("Unique user id: {}", uniqueUserId);
}

关于java - Spring框架Web套接字独特的@SendTo注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46706546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com