我需要访问一个 websocket 服务,它会在 24 小时后关闭一个打开的 websocket 连接。如何使用 Spring-Boot 2 和 Webflux 实现重新连接?
这是我到目前为止所拥有的(取自https://github.com/artembilan/webflux-websocket-demo):
@GetMapping(path = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> getStreaming() throws URISyntaxException {
ReactorNettyWebSocketClient client = new ReactorNettyWebSocketClient();
EmitterProcessor<String> output = EmitterProcessor.create();
Mono<Void> sessionMono = client.execute(new URI("ws://localhost:8080/echo"),
session -> session.receive()
.timeout(Duration.ofSeconds(3))
.map(WebSocketMessage::getPayloadAsText)
.subscribeWith(output)
.then());
return output.doOnSubscribe(s -> sessionMono.subscribe());
}
一旦连接丢失(不再输入 3 秒),就会抛出 TimeoutException。但是如何重新连接套接字?