1

我正在尝试使用@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 客户端没有收到任何东西。

有什么问题?

4

2 回答 2

9

现在我让它工作了,但我真的不知道为什么

配置:

@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("/queue", "/topic"); // <- added "/queue"
        registry.setApplicationDestinationPrefixes("/app");
        registry.setUserDestinationPrefix("/user"); 
    }
}

控制器:

@MessageMapping("/search")
@SendToUser // <- maps to "/user/queue/search"
public String search(@Payload String xxx) {
    return "TEST1234";
}

JS:

stompClient.subscribe('/user/queue/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=fkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 158 - Processing CONNECT session=fkbmnpkj
DEBUG      o.s.m.simp.user.UserDestinationMessageHandler: 187 - Translated /user/queue/search -> [/queue/search-userfkbmnpkj]
DEBUG       o.s.m.simp.broker.SimpleBrokerMessageHandler: 175 - Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

现在日志中多了一行,它说是Processing SUBSCRIBE /queue/search-userfkbmnpkj id=sub-0 session=fkbmnpkj

那是以前缺失的。

如果有人能解释为什么我需要/queue映射才能正常工作,那还是很好的。

于 2014-10-09T21:49:06.613 回答
0

在第一篇文章中,您在“/user/search”订阅。在第二个“/user/queue/search”中。我检查了“/user/topic/search”,它也有效。所以解决方案是使用有效的主题名称。没有魔法...

于 2014-12-19T15:06:59.350 回答