Spring Stomp @SendToUser的未经身份验证的用户无法正常工作

我正在尝试使用来响应未经身份验证的用户@SendToUser

  • spring4.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"));

spring输出:

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客户端什么也没收到。

有什么问题?

回答:

现在我可以使用它了,但是我真的不知道 :

回答:

@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";

}

回答:

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

现在,日志中还有一行代码,表示正在

那是以前没有的。

如果有人可以解释为什么我需要/queue映射才能正常工作,这仍然会很好。

以上是 Spring Stomp @SendToUser的未经身份验证的用户无法正常工作 的全部内容, 来源链接: utcz.com/qa/433227.html

回到顶部