我想发送消息给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'方法中找到断点? –