客户端:订阅来自不同服务器的多个websocket推送

问题描述 投票:0回答:1

我是 SockJS、Stomp 和 Spring websocket 的新手。 我的 spring websocket 配置如下:

@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/stomp-endpoint").setAllowedOriginPatterns("*").setAllowedOrigins().withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic", "/queue");  
    registry.setApplicationDestinationPrefixes("/app");
}

我正在我的

spring
应用程序中运行多个实例或部署上述实现。 当我在本地访问它时,客户端 JS 上的以下代码完美运行:

var socket = new SockJS("/stomp-endpoint");
stompClient = Stomp.over(socket);
stompClient.connect("", "", function (frame) {
        stompClient.subscribe("/topic/publish-updates", function (publishedUpdates) {
                            showMessages(JSON.parse(publishedUpdates.body));
        });
    });

现在,我想要如下所示的内容,以便我的单个 UI 代码将从两个服务器获取推送通知。

var socket1 = new SockJS("http://cml-consumerinstance-ci-0000.com:8080/stomp-endpoint");
stompClient1 = Stomp.over(socket);
stompClient1.connect("", "", function (frame) {
        stompClient1.subscribe("http://cml-consumerinstance-ci-0000.com:8080/topic/publish-updates", function (publishedUpdates) {
                            showMessages(JSON.parse(publishedUpdates.body));
        });
    });

var socket2 = new SockJS("http://cml-consumerinstance-ci-1111.com:8080/stomp-endpoint");
stompClient2 = Stomp.over(socket);
stompClient2.connect("", "", function (frame) {
        stompClient2.subscribe("http://cml-consumerinstance-ci-1111.com:8080/topic/publish-updates", function (publishedUpdates) {
                            showMessages(JSON.parse(publishedUpdates.body));
        });
    });

上面的代码不起作用。 我也尝试过使用

"http://cml-consumerinstance-ci-0000.dev3oke2phx.databasede3phx.oraclevcn.com:8080/app/topic/publish-updates"

有人可以帮我吗,如何实现不同服务器的多个订阅?

javascript websocket stomp spring-websocket sockjs
1个回答
0
投票

您应该订阅

/topic/publish-updates
,而不是
http://cml-consumerinstance-ci-1111.com:8080/topic/publish-updates

© www.soinside.com 2019 - 2024. All rights reserved.