使用 Spring Websocket 向特定用户发送消息
Sending messages to specific user with Spring Websocket
我使用本教程中描述的 spring 设置了一个 WebSocket:https://spring.io/guides/gs/messaging-stomp-websocket/。我需要的是我的服务器每 5 秒向特定用户发送一条消息。所以我首先做了这个:
@Autowired
private SimpMessagingTemplate template;
@Scheduled(fixedRate = 5000)
public void greet() {
template.convertAndSend("/topic/greetings", new Greeting("Bufff!"));
}
并且有效。现在只向特定用户发送消息我更改了以下内容:
@Scheduled(fixedRate = 5000)
public void greet() {
template.convertAndSendToUser("MyName","/queue/greetings", new Greeting("Bufff!"));
}
在 WebSocketConfig.java 中添加队列:
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic","/queue");
config.setApplicationDestinationPrefixes("/app");
}
更改 GreetingController.java 中的注释:
@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 中的连接函数:
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-boot-security,我通过使用 SimpUserRegistry ). But unfortunately my code is not working. I already tried this Sending message to specific user using spring but i do not want Spring to distinguish between sessions but users. I also looked at this Sending message to specific user on Spring Websocket 查找所有用户来确定 MyName
是否是正确的名称,但它没有帮助,因为 link 不起作用。
这是我的控制台日志:
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!"}
我是否错过了更改?
您能看到客户端成功订阅了您的端点吗?
我认为您缺少客户端代码中的第一个 /
'user/queue/greetings'
应该是 '/user/queue/greetings'
我使用本教程中描述的 spring 设置了一个 WebSocket:https://spring.io/guides/gs/messaging-stomp-websocket/。我需要的是我的服务器每 5 秒向特定用户发送一条消息。所以我首先做了这个:
@Autowired
private SimpMessagingTemplate template;
@Scheduled(fixedRate = 5000)
public void greet() {
template.convertAndSend("/topic/greetings", new Greeting("Bufff!"));
}
并且有效。现在只向特定用户发送消息我更改了以下内容:
@Scheduled(fixedRate = 5000)
public void greet() {
template.convertAndSendToUser("MyName","/queue/greetings", new Greeting("Bufff!"));
}
在 WebSocketConfig.java 中添加队列:
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic","/queue");
config.setApplicationDestinationPrefixes("/app");
}
更改 GreetingController.java 中的注释:
@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 中的连接函数:
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-boot-security,我通过使用 SimpUserRegistry MyName
是否是正确的名称,但它没有帮助,因为 link 不起作用。
这是我的控制台日志:
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!"}
我是否错过了更改?
您能看到客户端成功订阅了您的端点吗?
我认为您缺少客户端代码中的第一个 /
'user/queue/greetings'
应该是 '/user/queue/greetings'