mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-15 07:03:49 +08:00
WebFlux 中 WebSocket 实现通信
This commit is contained in:
19
springboot-webflux-8-websocket/src/test/java/WSClient.java
Normal file
19
springboot-webflux-8-websocket/src/test/java/WSClient.java
Normal file
@@ -0,0 +1,19 @@
|
||||
import org.springframework.web.reactive.socket.WebSocketMessage;
|
||||
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
|
||||
import org.springframework.web.reactive.socket.client.WebSocketClient;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
public class WSClient {
|
||||
public static void main(final String[] args) {
|
||||
final WebSocketClient client = new ReactorNettyWebSocketClient();
|
||||
client.execute(URI.create("ws://localhost:8080/echo"), session ->
|
||||
session.send(Flux.just(session.textMessage("你好")))
|
||||
.thenMany(session.receive().take(1).map(WebSocketMessage::getPayloadAsText))
|
||||
.doOnNext(System.out::println)
|
||||
.then())
|
||||
.block(Duration.ofMillis(5000));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user