在spring boot websocket中向特定用户发送通知 [英] Send Notification to specific user in spring boot websocket

查看:2186
本文介绍了在spring boot websocket中向特定用户发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to send notification to specific client.

例如用户名用户

@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 Service

Angular Service

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和这里 https://stackoverflow.com/questions/25646671/check-auth-while-sending-a-message-to-a-specific-user-by-using-stomp-and-websock/25647822#25647822>这里

I am have searched many other questions and tried but none of them worked for me e.g 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

提前谢谢。

推荐答案

gerrytan <的答案/ a> 向Spring Websocket上的特定用户发送消息提到Web套接字配置更改,注册 / user 前缀。在你的情况下,我想这意味着要替换

The answer of gerrytan to Sending message to specific user on Spring Websocket mentions a web socket configuration change, to register the /user prefix. In your case I guess it means to replace

registry.enableSimpleBroker("/topic", "/queue");

with

registry.enableSimpleBroker("/topic", "/queue", "/user");

他还说在控制器中你不需要 / user 前缀因为它是自动添加的。所以你可以试试这个:

He also says that in controller you don't need the /user prefix because it is added automatically. So you could try this:

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

这个:

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

在客户端,您需要提供用于连接服务器的用户名。您可以直接插入:

On the client side you need to provide the username that you used to connect to server. You might insert it directly:

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

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

or get it from a header. But Markus says at How to send websocket message to concrete user? that even here you don't need the username, so it might work like this:

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

这篇关于在spring boot websocket中向特定用户发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆