我正在尝试使用@SendToUser
.
- 春天 4.1.1
我正在使用一个新创建的 Spring Boot 应用程序,我唯一的配置是:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/stomp").withSockJS();
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/topic");
registry.setApplicationDestinationPrefixes("/app");
registry.setUserDestinationPrefix("/user");
}
}
控制器代码:
@MessageMapping("/search")
@SendToUser("/search")
public String search(@Payload String xxx) {
return "TEST1234";
}
JS:
var socket = new SockJS('/webapp/stomp');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
stompClient.subscribe('/user/search', function(data){
alert(data.body);
});
});
弹簧输出:
DEBUG org.springframework.web.servlet.DispatcherServlet: 996 - Successfully completed request
DEBUG o.s.w.s.handler.LoggingWebSocketHandlerDecorator: 45 - New WebSocketServerSockJsSession[id=shizav88]
DEBUG o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=shizav88
DEBUG o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/search -> [/search-usershizav88]
JS:
var name = document.getElementById('name').value;
stompClient.send("/app/search", {}, "test"));
弹簧输出:
DEBUG o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 348 - Searching methods to handle SEND /app/search session=shizav88
DEBUG o.s.m.s.a.s.SimpAnnotationMethodMessageHandler: 446 - Invoking de.benneq.next.search.SearchController#search[1 args]
DEBUG o.s.b.factory.support.DefaultListableBeanFactory: 247 - Returning cached instance of singleton bean 'searchController'
DEBUG o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/shizav88/search -> [/search-usershizav88]
在我看来,Spring 将订阅映射到/search-usershizav88
. 然后用于/search-usershizav88
发送响应。但是我的 JS 客户端没有收到任何东西。
有什么问题?