给出websocket配置:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.setApplicationDestinationPrefixes("/app");
registry.enableSimpleBroker("/queue", "/topic");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/add").withSockJS();
}
}
和客户端javascript:
<script type="text/javascript">
console.log('begin javascript');
var stompClient = null;
function connect() {
var socket = new SockJS('/myapp/add');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
console.log('Connected to STOMP: ' + frame);
stompClient.subscribe('/user/topic/abc', function(calResult) {
console.log('*** Got it ***');
});
});
}
connect();
</script>
并从服务器发送此消息:
messagingTemplate.convertAndSendToUser(username, "/topic/abc", "hello");
回调永远不会被解雇。
javascript控制台显示已建立连接:
连接到STOMP:CONNECTED用户名:jschmoe heart-beat:0,0 版本:1.1 SUBSCRIBE id:sub-0 destination:/ user / topic / abc
并且tomcat控制台显示:
处理SUBSCRIBE目的地= / topic / abc-useryl3ovhr2 subscriptionId = sub-0 session = yl3ovhr2 user = jschmoe payload = byte [0]
然后发送消息:
处理MESSAGE目的地= / topic / abc-useryl3ovhr2 session = null 有效载荷=你好
似乎一切都有效,除了回调。
答案 0 :(得分:1)
在我的情况下,问题是由XML配置引起的,一旦我用@EnableWebSocketMessageBroker
注释切换到Java配置,我就收到了客户端的消息。