Java 如何检测SpringWebSocket stomp订阅消息(帧)?
我正在使用Spring5:如何检测来自Stomp客户端的Java 如何检测SpringWebSocket stomp订阅消息(帧)?,java,spring,spring-websocket,spring-messaging,Java,Spring,Spring Websocket,Spring Messaging,我正在使用Spring5:如何检测来自Stomp客户端的SUBSCRIBE消息 根据我的理解,@SubscribeMapping应该使我的控制器方法在客户机订阅主题时被调用,但事实并非如此 这是我的服务器控制器: @Controller public class MessageController { // ... @MessageMapping("/chat/{mId}") @SendTo("/topic/messages") public OutputMe
SUBSCRIBE
消息
根据我的理解,@SubscribeMapping
应该使我的控制器方法在客户机订阅主题时被调用,但事实并非如此
这是我的服务器控制器:
@Controller
public class MessageController {
// ...
@MessageMapping("/chat/{mId}")
@SendTo("/topic/messages")
public OutputMessage send(Message message, @DestinationVariable("mId") String mid, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception {
// ...
}
@SuppressWarnings("unused")
@SubscribeMapping({ "/", "/chat", "/topic/messages", "/messages", "/*" })
public void listen(Message message, MessageHeaders headers, MessageHeaderAccessor accessor) throws Exception {
int i = 0;
System.out.println("subscribed");
}
}
服务器配置:
@Configuration
@ComponentScan(basePackages= { "websockets" })
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/chat");
registry.addEndpoint("/chat").withSockJS();
}
@Override
public void configureWebSocketTransport(WebSocketTransportRegistration registry) {
WebSocketMessageBrokerConfigurer.super.configureWebSocketTransport(registry);
}
}
和javascript客户端:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Chat WebSocket</title>
<script src="sockjs.js"></script>
<script src="stomp.js"></script>
<script type="text/javascript">
// ...
function connect() {
var sock = new SockJS('/<webapp-context>/chat');
stompClient = Stomp.over(sock);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/messages', function(messageOutput) {
showMessageOutput(JSON.parse(messageOutput.body));
});
stompClient.subscribe('/topic/messages/13', function(messageOutput) {
showMessageOutput(JSON.parse(messageOutput.body));
});
});
}
// ...
</script>
</head>
<body onload="/*disconnect()*/">
<!-- ... -->
</body>
</html>
聊天网站
// ...
函数连接(){
var sock=new SockJS('//chat');
stompClient=Stomp.over(sock);
stompClient.connect({},函数(框架){
setConnected(true);
console.log('Connected:'+frame);
stompClient.subscribe('/topic/messages',函数(messageOutput){
showMessageOutput(JSON.parse(messageOutput.body));
});
stompClient.subscribe('/topic/messages/13',函数(messageOutput){
showMessageOutput(JSON.parse(messageOutput.body));
});
});
}
// ...
该代码已从中改编
如和中所述,我可以只使用拦截器,但如何工作呢?您需要将“topic”也注册为应用程序目标topicconfig.setApplicationDestinationPrefixes({/app“,“/topic”})代码>
否则Spring不会将订阅消息转发到应用程序,而只是将其转发到MessageBroker通道。您需要将“topic”也注册为应用程序目标topicconfig.setApplicationDestinationPrefixes({/app“,“/topic”})代码>
否则Spring不会将subscribe消息转发到应用程序,而只是将其转发到MessageBroker频道。就是这样!谢谢。给读者一个小提示:Spring在转发到应用程序之前去掉了/topic
前缀。就是这样!谢谢。请读者注意:Spring在转发到应用程序之前去掉了/topic
前缀。