2016-06-29 151 views
0

我想发送消息给websocket订阅者一个特定的记录 - 当一个动作发生在我的一个服务类中时。Spring发送消息给Websocket Message Broker

我正在尝试阅读Spring Websocket documentation,但它对于如何让所有这些事情一起工作有点含糊不清。

这里是我的设置文件(这是BTW扩展jHipster):

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

@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(); 
} 

控制器类(在试图实现一个简单的经纪人我可以测试从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); 
    // ... 
} 

当我在public void activity方法上放置一个断点时,我什么都没得到?

+0

您能分享sockJS客户端日志吗?没有办法看到哪些消息被发送以及它得到的回应是什么。你还可以把'org.springframework.web.socket'放在DEBUG中,并分享相关的日志吗? –

+0

@BrianClozel - 我还没有订阅频道 - 会导致它不活跃?!我想在运行'convertAndSend'后,我应该在'void activity'方法中找到断点? –

回答

0

使用消息传递模板向"/topic/commandcenterID"发送消息会将该消息发送给消息代理,消息代理会将该消息分发给订阅了该主题的客户端。所以它不会流经你的活动方法。

当使用@MessageMapping带注释的方法时,您将它们声明为应用程序目标。因此,向"/app/ws发送消息“应映射到该方法。请注意,在这种情况下,我怀疑它会起作用,因为@MessageMapping注释中的路径定义中缺少您期望作为方法参数的目标变量 另外,在@SendTo注释其实告诉Spring,通过该方法返回的值应转换为一个消息,并发送至指定目的地

看来你混的东西在这里,我想你应该:

相关问题