如何通过simpMessagingTemplate.convertAndSendToUser将消息发送给特定用户?

时间:2019-06-11 10:11:43

标签: spring-boot websocket stomp

我正在开发一个应用程序,其中有关管理员执行的某些操作的通知会发送给特定用户。我正在使用网络套接字进行实时通信,但是问题是我的队列无法将消息发送给指定的用户。

我正在将stomp js与spring-boot一起使用,以将消息从后端发送到前端,这是一个有角度的应用程序。当我对主题进行操作时,它工作正常,但不适用于队列。请参阅下面的代码。 我尝试通过名称,电子邮件甚至ID发送邮件。但该邮件未到达预期的用户,或者我应该说我不知道​​它向何处发送或向哪个目的地发送。

控制器代码

public void sendNotificationToUser(@Payload Request response) {
        log.info("test user");
        simpMessagingTemplate.convertAndSendToUser(<tried with Id, with name, with e-mail>, "/queue/notice", response );
    }

我的配置

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/notification").setAllowedOrigins("*").withSockJS();
    }


    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
        config.enableSimpleBroker("/topic", "/queue");
    }
}

接受消息的角度代码。

 _this.stompClient.subscribe("/queue/notice", function(data){
            console.log(data);
            _this.notifications.emit(data);
          }, {});
        }

我还尝试通过标头发送ID或sessionId或用户名。结果相同。

我希望当我从后端发送消息时,它应该到达预期的用户,就像通过网络套接字进行聊天的情况一样,但是当前该消息没有发送。 我需要为用户和管理员使用单独的消息映射,还是在配置中缺少某些内容?

0 个答案:

没有答案
相关问题