是否可以在多个websocket流和可控订阅上订阅websocket会话

时间:2018-10-23 12:16:10

标签: java websocket spring-webflux

想使用Spring Web Flux实现功能。所有应用程序客户端都通过websocket连接到客户端服务。然后想要将他们的会话订阅到另一个微服务的websocket流,并根据传入消息管理订阅。

@Component
public class ReactiveWebSocketHandler implements WebSocketHandler {

@Override
@NotNull
public Mono<Void> handle(@NotNull WebSocketSession session) {
    final WebSocketClient client = new ReactorNettyWebSocketClient();
    final URI url1 = URI.create("ws://another-service1");
    final URI url2 = URI.create("ws://another-service2");

    return session.receive()
            .doOnNext(message -> {
                final String command = message.getPayloadAsText();
                if (command.equals("subscribe sevice1")) {
                    // client.execute(url1, ...
                    // get flux stream from client invoke and start sending it to current session
                } else if (command.equals("subscribe sevice2")) {
                    // ...
                } else if (command.equals("unsubscribe sevice1")) {
                    // ...
                } else if (command.equals("unsubscribe sevice2")) {
                    // ...
                }
            })
            .then(); 
} 

是否可以使用webflux实现这种逻辑?

0 个答案:

没有答案