我的Spring Boot应用程序中具有以下websocket配置和终结点:
控制器:
@Controller
public class QuoteController {
@Autowired
private RabbitTemplate rabbitTemplate;
@SubscribeMapping("/quote")
public void singleQuote() {
System.out.println("Salam");
}
}
和配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {
public static final String WEBSOCKET_HANDSHAKE_ENDPOINT_URI = "/api/wsocket";
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic"); /*Enable a simple in-memory broker for the clients to subscribe to channels and receive messages*/
config.setApplicationDestinationPrefixes("/ws"); /*The prefix for the endpoints in the controller*/
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
/*websocket handshaking endpoint*/
registry.addEndpoint(WEBSOCKET_HANDSHAKE_ENDPOINT_URI)
/*TODO remove this after development*/
.setAllowedOrigins("http://localhost:8080");
}
}
以及基于Web的js客户端:
this.client = new Client();
this.client.configure({
brokerURL: `ws://localhost:8553/api/wsocket`,
onConnect: () => {
console.log('onConnect');
this.client.subscribe('/topic/quote', message => {
console.log(message);
debugger;
});
},
// Helps during debugging, remove in production
debug: (str) => {
console.log(new Date(), str);
}
});
this.client.activate();
我希望singleQuote
被击中,但没有击中。为什么?客户端订阅频道时,不是应该调用带有SubscribeMapping
注释的方法吗?