问题 在spring boot websocket中向特定用户发送通知


我想向特定客户发送通知。

例如用户名用户

@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 !";
}

客户端

角度服务

connect() {
    let socket = new SockJs(`api/socket`);

    let stompClient = Stomp.over(socket);

    return stompClient;
}

角度分量

 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;

      })     });

我已经搜索了许多其他问题但尝试过,但没有一个能为我工作 例如 这里 Thanh Nguyen Van回答  这里 

安慰

 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

提前致谢 。


11939
2018-04-10 08:06


起源



答案:


的答案 gerrytan 至 在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', ...)

或者从标题中获取它。但 马库斯 说 如何向具体用户发送websocket消息? 即使在这里你不需要用户名,所以它可能会这样工作:

stompClient.subscribe('/user/queue/notification', ...)

7
2018-04-30 23:43





好像你错过了目的地的斜线:

template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications); 

6
2018-04-10 08:47



不,我已经尝试过了。 - naila naseem
你想提出另一个建议吗?谢谢 - naila naseem
查看Spring Websocket Chat示例,可能会对您有所帮助 github.com/salmar/spring-websocket-chat - Sergi Almar
还没有解决 - naila naseem
我可以在spring template.convertAndSendToUser(.....)中找到此行生成的路径吗? - naila naseem


答案:


的答案 gerrytan 至 在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', ...)

或者从标题中获取它。但 马库斯 说 如何向具体用户发送websocket消息? 即使在这里你不需要用户名,所以它可能会这样工作:

stompClient.subscribe('/user/queue/notification', ...)

7
2018-04-30 23:43





好像你错过了目的地的斜线:

template.convertAndSendToUser(principal.getName(), "/queue/notification", notifications); 

6
2018-04-10 08:47



不,我已经尝试过了。 - naila naseem
你想提出另一个建议吗?谢谢 - naila naseem
查看Spring Websocket Chat示例,可能会对您有所帮助 github.com/salmar/spring-websocket-chat - Sergi Almar
还没有解决 - naila naseem
我可以在spring template.convertAndSendToUser(.....)中找到此行生成的路径吗? - naila naseem