简体   繁体   English

Spring将消息发送到Websocket Message Broker

[英]Spring send message to Websocket Message Broker

I want to send a message to websocket subscribers of a specific record - when an action takes place in one of my service class. 我想向特定记录的websocket订阅者发送消息-当在我的服务类别之一中发生操作时。

I'm trying to read the Spring Websocket documentation but it's kind of ambiguous to the point of how to get all these things working together. 我正在尝试阅读Spring Websocket文档,但是如何使所有这些东西协同工作有点含糊。

Here are my setup files (this is extending jHipster btw): 这是我的安装文件(这是对jHipster btw的扩展):

WebsocketConfiguration.java WebsocketConfiguration.java

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableStompBrokerRelay("/queue/", "/topic/", "/exchange/");
        config.setApplicationDestinationPrefixes("/app");
        config.setPathMatcher(new AntPathMatcher("."));
    }

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

WebsocketSecurity.java WebsocketSecurity.java

@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
    messages
        // message types other than MESSAGE and SUBSCRIBE
        .nullDestMatcher().authenticated()
        // matches any destination that starts with /rooms/
        .simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN)
         .simpDestMatchers("/topic/**").authenticated()
        // (i.e. cannot send messages directly to /topic/, /queue/)
        // (i.e. cannot subscribe to /topic/messages/* to get messages sent to
        // /topic/messages-user<id>)
         .simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll()
        // catch all
        .anyMessage().denyAll();
}

Controller class (attempt at implementing a simple broker I can test subscribing to from sockjs and recieving messages generated elsewhere in the application: 控制器类(尝试实现一个简单的代理,我可以测试从sockjs订阅并接收应用程序中其他位置生成的消息:

@MessageMapping("/ws")
@SendTo("/topic/sendactivity.{id}")
public void activity(@DestinationVariable string id, @Payload String message){
    log.debug("Sending command center: "+message);
}

@RequestMapping(value = "/updateactivity", method = RequestMethod.PUT)
public ResponseEntity<Membership> updateMembership(
        @RequestBody Membership membership) throws URISyntaxException {
    // ...
    String testString = "test";
    messagingTemplate.convertAndSend("/topic/commandcenter"+membership.getId().toString(), testString);
    // ...
}

When I put a breakpoint on the public void activity method, I don't get anything? 当我在public void activity方法上设置断点时,我什么都没得到?

Sending a message to "/topic/commandcenterID" using the messaging template will send that message to the message broker, which will dispatch that message to clients subscribed to that topic. 使用消息传递模板将消息发送到"/topic/commandcenterID"会将消息发送到消息代理,消息代理将消息分发给订阅该主题的客户端。 So it won't flow through your activity method. 因此,它不会流经您的活动方法。

When using @MessageMapping annotated methods, you're declaring those as application destinations. 使用@MessageMapping批注方法时,您需要将其声明为应用程序目标。 So sending a message to "/app/ws " should map to that method. 因此,将消息发送到"/app/ws ”应该映射到该方法。 Note that in that case I doubt it'll work since the destination variable you're expecting as a method argument is missing from the path definition in the @MessageMapping annotation. 请注意,在那种情况下,我怀疑它是否会工作,因为@MessageMapping批注中的路径定义中缺少您期望作为方法参数的目标变量。 Also, the @SendTo annotation in fact tells Spring that the value returned by the method should be converted to a message and sent to the given destination. 同样, @SendTo注释实际上告诉Spring,该方法返回的值应转换为消息并发送到给定的目的地。

It seems you're mixing things up here, and I think you should: 看来您正在把事情混在一起,我认为您应该:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM