2016-06-29 236 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,通過該方法返回的值應轉換爲一個消息,併發送至指定目的地

看來你混的東西在這裏,我想你應該:

相關問題