在 spring 引导 websocket 中向特定用户发送通知
Send Notification to specific user in spring boot websocket
我想向特定客户发送通知。
例如用户名 user
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends
AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic", "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
控制器
@GetMapping("/notify")
public String getNotification(Principal principal) {
String username = "user";
notifications.increment();
logger.info("counter" + notifications.getCount() + "" + principal.getName());
// logger.info("usersend:"+sha.getUser().getName()) ; //user
template.convertAndSendToUser(principal.getName(), "queue/notification", notifications);
return "Notifications successfully sent to Angular !";
}
客户端
Angular 服务
connect() {
let socket = new SockJs(`api/socket`);
let stompClient = Stomp.over(socket);
return stompClient;
}
Angular 组件
let stompClient = this.webSocketService.connect();
stompClient.connect({}, frame => {
stompClient.subscribe('/user/queue/notification', notifications => {
console.log('test'+notifications)
this.notifications = JSON.parse(notifications.body).count;
}) });
我搜索了许多其他问题并进行了尝试,但其中 none 对我有用
例如 here answered by Thanh Nguyen Van and here
控制台
Opening Web Socket...
stomp.js:134 Web Socket Opened...
stomp.js:134 >>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.js:134 <<< CONNECTED
version:1.1
heart-beat:0,0
stomp.js:134 connected to server undefined
reminder.component.ts:18 test callsed
stomp.js:134 >>> SUBSCRIBE
id:sub-0
destination:/user/queue/notification
提前致谢。
您的目的地似乎缺少斜线:
template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications);
gerrytan to Sending message to specific user on Spring Websocket 的答案提到了 web 套接字配置更改,以注册 /user
前缀。在你的情况下,我想这意味着替换
registry.enableSimpleBroker("/topic", "/queue");
和
registry.enableSimpleBroker("/topic", "/queue", "/user");
他还说在控制器中你不需要 /user
前缀,因为它是自动添加的。所以你可以试试这个:
template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications);
还有这个:
template.convertAndSendToUser(principal.getName(), "/user/queue/notification", notifications);
在客户端,您需要提供用于连接服务器的用户名。您可以直接插入:
stompClient.subscribe('/user/naila/queue/notification', ...)
或从 header 获取。但是 Markus says at How to send websocket message to concrete user? 即使在这里你也不需要用户名,所以它可能像这样工作:
stompClient.subscribe('/user/queue/notification', ...)
我想向特定客户发送通知。
例如用户名 user
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends
AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
stompEndpointRegistry.addEndpoint("/socket")
.setAllowedOrigins("*")
.withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic", "/queue");
registry.setApplicationDestinationPrefixes("/app");
}
控制器
@GetMapping("/notify")
public String getNotification(Principal principal) {
String username = "user";
notifications.increment();
logger.info("counter" + notifications.getCount() + "" + principal.getName());
// logger.info("usersend:"+sha.getUser().getName()) ; //user
template.convertAndSendToUser(principal.getName(), "queue/notification", notifications);
return "Notifications successfully sent to Angular !";
}
客户端
Angular 服务
connect() {
let socket = new SockJs(`api/socket`);
let stompClient = Stomp.over(socket);
return stompClient;
}
Angular 组件
let stompClient = this.webSocketService.connect();
stompClient.connect({}, frame => {
stompClient.subscribe('/user/queue/notification', notifications => {
console.log('test'+notifications)
this.notifications = JSON.parse(notifications.body).count;
}) });
我搜索了许多其他问题并进行了尝试,但其中 none 对我有用 例如 here answered by Thanh Nguyen Van and here
控制台
Opening Web Socket...
stomp.js:134 Web Socket Opened...
stomp.js:134 >>> CONNECT
accept-version:1.1,1.0
heart-beat:10000,10000
stomp.js:134 <<< CONNECTED
version:1.1
heart-beat:0,0
stomp.js:134 connected to server undefined
reminder.component.ts:18 test callsed
stomp.js:134 >>> SUBSCRIBE
id:sub-0
destination:/user/queue/notification
提前致谢。
您的目的地似乎缺少斜线:
template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications);
gerrytan to Sending message to specific user on Spring Websocket 的答案提到了 web 套接字配置更改,以注册 /user
前缀。在你的情况下,我想这意味着替换
registry.enableSimpleBroker("/topic", "/queue");
和
registry.enableSimpleBroker("/topic", "/queue", "/user");
他还说在控制器中你不需要 /user
前缀,因为它是自动添加的。所以你可以试试这个:
template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications);
还有这个:
template.convertAndSendToUser(principal.getName(), "/user/queue/notification", notifications);
在客户端,您需要提供用于连接服务器的用户名。您可以直接插入:
stompClient.subscribe('/user/naila/queue/notification', ...)
或从 header 获取。但是 Markus says at How to send websocket message to concrete user? 即使在这里你也不需要用户名,所以它可能像这样工作:
stompClient.subscribe('/user/queue/notification', ...)