Spring Boot 如何推送或发布多条消息并保持会话打开

wf82jlnq  于 2023-03-23  发布在  Spring
关注(0)|答案(1)|浏览(160)

如何在Spring中将多个消息相继推送或发布到同一个Web-socket Session?

public Flux<WebSocketSession> openWebSocket() {
Flux<String> input = Flux.just("{\"action\":\"auth\",\"params\":\"SomeKEY AUTH\"}\n",
        "{{\"action\":\"subscribe\", \"params\":\"subcriptionmessage.*\"}");
Flux<String> input1 = Flux.just("{{\"action\":\"subscribe\", \"params\":\"AM.*\"}");
Flux<String> input2 = Flux.just("{\"action\":\"auth\",\"params\":\"SomeKEY AUTH\"}\n");
ReplayProcessor<Object> output = ReplayProcessor.create(100);

try {
    webSocketClient.execute(new URI("wss://***"),
            session -> {
                log.debug("Starting to send messages");
                return session
                        .send(input.doOnNext(s -> log.debug("outbound " + s)).map(session::textMessage))
                        .thenMany(session.receive().map(WebSocketMessage::getPayloadAsText))
                        .delayUntil(i -> Mono.delay(Duration.ofSeconds(5)))
                        .doOnNext(s -> log.debug("inbound " + s))
                        .subscribeWith(output).then();
            }).block();
} catch (URISyntaxException e) {
    throw new RuntimeException(e);

}
return null;

}

zpgglvta

zpgglvta1#

我纠正了我的有效载荷和它的作品这是一个问题的有效载荷,有无效的json输入字符串
实际:“{{“action”:“subscribe”,“params”:“subcriptionmessage."}”
应为:“{“action”:“subscribe”,“params”:“subcriptionmessage.
"}”

相关问题