我想發送消息給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
方法上放置一個斷點時,我什麼都沒得到?
您能分享sockJS客戶端日誌嗎?沒有辦法看到哪些消息被髮送以及它得到的迴應是什麼。你還可以把'org.springframework.web.socket'放在DEBUG中,並分享相關的日誌嗎? –
@BrianClozel - 我還沒有訂閱頻道 - 會導致它不活躍?!我想在運行'convertAndSend'後,我應該在'void activity'方法中找到斷點? –