gpt4 book ai didi

javascript - 未调用 Spring Stomp Websockets 回调

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:05 26 4
gpt4 key购买 nike

给定一个 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 控制台显示连接已建立:

Connected to STOMP: CONNECTED user-name:jschmoe heart-beat:0,0 version:1.1 SUBSCRIBE id:sub-0 destination:/user/topic/abc

并且 tomcat 控制台显示:

Processing SUBSCRIBE destination=/topic/abc-useryl3ovhr2 subscriptionId=sub-0 session=yl3ovhr2 user=jschmoe payload=byte[0]

然后在发送消息时:

Processing MESSAGE destination=/topic/abc-useryl3ovhr2 session=null payload=hello

似乎除了回调之外一切正常。

最佳答案

在我的例子中,问题是由 XML 配置引起的,一旦我切换到带有 @EnableWebSocketMessageBroker 注释的 Java 配置,我就在客户端收到了消息。

关于javascript - 未调用 Spring Stomp Websockets 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33402717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com