弹簧 websocket 通知 [英] spring websocket notification
问题描述
好的,我现在正在遵循 spring 站点上的指南,但我在如何使用 WebSocket 仅向一个用户发送通知方面遇到问题,我正在遵循本指南 https://spring.io/guides/gs/messaging-stomp-websocket/.我想要的是:我有 2 个用户,他们都订阅了 process1...User1 需要让服务器处理他的通行证...现在我希望服务器将通知只发送给 User1...
Ok here i'm , i'm right now following the guides on spring site but i'm having problem on how to deliver a notification to only one user using WebSocket, i'm following this guide https://spring.io/guides/gs/messaging-stomp-websocket/ . What I want is: i have 2 users, both of them subscribe to process1... User1 need to let the server process his pass...now i want that the server will deliver the notification only to User1...
@Controller
public class ProcessController {
@MessageMapping("/ProcessOwner/approve/{pass}")
@SendTo("")
public String notifica(@DestinationVariable String pass)throws Exception{
return "ok"+pass;
}
}
现在我应该在@SendTo 字段中写什么来仅将答案传递给 user1 ?如果写/Process/process1 写不好,user1 和 user2 都会收到消息....
Now what should i write in the @SendTo field to deliver the answer only to user1 ? if ill write /Process/process1 both user1 and user2 will receive the message....
推荐答案
您可以使用后缀.它发送给每个唯一的客户.
You ca use the sufix. It's send to every unique client.
在js代码中订阅时:
client.connect('guest', 'guest', function(frame) {
var suffix = frame.headers['queue-suffix'];
client.subscribe("/queue/error" + suffix, function(msg) {
// handle error
});
client.subscribe("/queue/position-updates" + suffix, function(msg) {
// handle position update
});
});
在服务器端,您可以使用@ReplyToUser
或消息模板
On the server side you can use @ReplyToUser
or the message template
String user = "fabrice";
String queue = "/queue/position-updates";
this.messagingTemplate.convertAndSendToUser(user, queue, position);
在此处查看更多信息:http://assets.spring.io/wp/WebSocketBlogPost.html(部分:向单个用户发送消息)
See more here: http://assets.spring.io/wp/WebSocketBlogPost.html (section: Sending Messages To a Single User)
这篇关于弹簧 websocket 通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!