当 SessionSubscribeEvent 发生时,stomp 客户端没有得到 messages/notifications

stomp client is not getting messages/notifications when SessionSubscribeEvent is happend

我有一个订阅听众

@Service
public class UserSubscribeEventListenerService {

    private SimpMessagingTemplate template;
    @Autowired
    private GenericService<User> userService;

    @Autowired
    public UserSubscribeEventListenerService(SimpMessagingTemplate template) {
        this.template = template;
    }

    @EventListener
    public void handleSubscribeEvent(SessionSubscribeEvent event) {
        try {
            String subscribedUser = event.getUser().getName();
            System.out.println("User is " + event.getUser().getName() + " Message is " + "GREETINGS" );
            Thread.sleep(3000);
            System.out.println("AFTER 3 seconds...." );
            template.convertAndSendToUser(subscribedUser, "/user/"+subscribedUser+ "/notify", userService.getByEmail(subscribedUser));
        } catch (MessagingException e) {
            System.out.println(e.getCause() 
                    + "\n\n"
                    + e.getCause());
            return;
        } catch (InterruptedException e) {
            System.out.println(e.getCause() 
                    + "\n\n"
                    + e.getCause());
            return;
        }
    }       
}

但是客户端是下面给出的一堆用于 stomp 的 JS 函数

<script type="text/javascript">     
    //Create stomp client over sockJS protocol
    var socket = new SockJS("/ws");
    ///var socket = new SockJS("/ws");
    var stompClient = Stomp.over(socket);
     $( document ).ready(function() {
         stompClient.connect({}, function(s) {    

                stompClient.subscribe('/user/' + s.headers['user-name'] + '/notify', function(frame){
                    var messages = JSON.parse(frame.body)

                      var i;
                      var count = 0;
                      for( i in messages) {
                          var message = messages[i]; 
                          count ++;
                          if(count <= 5){
                              $('#user_message_addon').append(
                                $('<li>').append(                                       
                                      $('<span></span>').html(message.notificationMessage)
                                    ),
                                    $('</li>')      
                                )
                              );
                          }
                        }                            
                });             

            });
        });
    </script>

似乎无法正常工作,在 UserSubscribeEventListenerService 事件发生时不显示或说不接收服务器发送的消息我在客户端获得控制台输出为

但我无法获得 message/notifications。

任何人都可以告诉我如何解决这个问题吗?

您是否尝试将 convertAndSendToUser 参数更改为:

template.convertAndSendToUser(subscribedUser, "/notify", userService.getByEmail(subscribedUser));

template.convertAndSendToUser(subscribedUser, "/user/notify", userService.getByEmail(subscribedUser));

我认为 spring 根据用户名更改队列名称本身。 或者您可以使用 convertAndSend 方法:

template.convertAndSend("/"+subscribedUser+"/notify", userService.getByEmail(subscribedUser));

template.convertAndSend("/user/"+subscribedUser+"/notify", userService.getByEmail(subscribedUser));

这取决于您的 websocket 配置(您可以将其包含在您的问题中吗?)