Search in sources :

Example 1 with WebSocketClient

use of org.springframework.web.reactive.socket.client.WebSocketClient in project tutorials by eugenp.

the class ReactiveJavaClientWebSocket method main.

public static void main(String[] args) throws InterruptedException {
    WebSocketClient client = new ReactorNettyWebSocketClient();
    client.execute(URI.create("ws://localhost:8080/event-emitter"), session -> session.send(Mono.just(session.textMessage("event-spring-reactive-client-websocket"))).thenMany(session.receive().map(WebSocketMessage::getPayloadAsText).log()).then()).block(Duration.ofSeconds(10L));
}
Also used : ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) Duration(java.time.Duration) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) Mono(reactor.core.publisher.Mono) URI(java.net.URI) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient)

Example 2 with WebSocketClient

use of org.springframework.web.reactive.socket.client.WebSocketClient in project spring-framework by spring-projects.

the class AbstractWebSocketIntegrationTests method arguments.

static Stream<Object[]> arguments() throws IOException {
    WebSocketClient[] clients = new WebSocketClient[] { new TomcatWebSocketClient(), new JettyWebSocketClient(), new ReactorNettyWebSocketClient(), new UndertowWebSocketClient(Xnio.getInstance().createWorker(OptionMap.EMPTY)) };
    Map<HttpServer, Class<?>> servers = new LinkedHashMap<>();
    servers.put(new TomcatHttpServer(TMP_DIR.getAbsolutePath(), WsContextListener.class), TomcatConfig.class);
    servers.put(new JettyHttpServer(), JettyConfig.class);
    servers.put(new ReactorHttpServer(), ReactorNettyConfig.class);
    servers.put(new UndertowHttpServer(), UndertowConfig.class);
    // Try each client once against each server..
    Flux<WebSocketClient> f1 = Flux.fromArray(clients).concatMap(c -> Mono.just(c).repeat(servers.size() - 1));
    Flux<Map.Entry<HttpServer, Class<?>>> f2 = Flux.fromIterable(servers.entrySet()).repeat(clients.length - 1).share();
    return Flux.zip(f1, f2.map(Map.Entry::getKey), f2.map(Map.Entry::getValue)).map(Tuple3::toArray).toStream();
}
Also used : WsContextListener(org.apache.tomcat.websocket.server.WsContextListener) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) TomcatWebSocketClient(org.springframework.web.reactive.socket.client.TomcatWebSocketClient) UndertowHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.UndertowHttpServer) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) ReactorNettyWebSocketClient(org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient) TomcatWebSocketClient(org.springframework.web.reactive.socket.client.TomcatWebSocketClient) UndertowWebSocketClient(org.springframework.web.reactive.socket.client.UndertowWebSocketClient) JettyWebSocketClient(org.springframework.web.reactive.socket.client.JettyWebSocketClient) LinkedHashMap(java.util.LinkedHashMap) JettyWebSocketClient(org.springframework.web.reactive.socket.client.JettyWebSocketClient) TomcatHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer) ReactorHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer) HttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer) JettyHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyHttpServer) UndertowHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.UndertowHttpServer) TomcatHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer) UndertowWebSocketClient(org.springframework.web.reactive.socket.client.UndertowWebSocketClient) OptionMap(org.xnio.OptionMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JettyHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyHttpServer)

Example 3 with WebSocketClient

use of org.springframework.web.reactive.socket.client.WebSocketClient in project spring-framework by spring-projects.

the class WebSocketIntegrationTests method customHeader.

@ParameterizedWebSocketTest
void customHeader(WebSocketClient client, HttpServer server, Class<?> serverConfigClass) throws Exception {
    startServer(client, server, serverConfigClass);
    HttpHeaders headers = new HttpHeaders();
    headers.add("my-header", "my-value");
    AtomicReference<Object> headerRef = new AtomicReference<>();
    this.client.execute(getUrl("/custom-header"), headers, session -> session.receive().map(WebSocketMessage::getPayloadAsText).doOnNext(headerRef::set).doOnError(headerRef::set).then()).block(TIMEOUT);
    assertThat(headerRef.get()).isEqualTo("my-header:my-value");
}
Also used : Retry(reactor.util.retry.Retry) HttpHeaders(org.springframework.http.HttpHeaders) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) ServerWebExchangeContextFilter(org.springframework.web.filter.reactive.ServerWebExchangeContextFilter) TomcatHttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpServer(org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer) Configuration(org.springframework.context.annotation.Configuration) Flux(reactor.core.publisher.Flux) List(java.util.List) WebFilter(org.springframework.web.server.WebFilter) Duration(java.time.Duration) Map(java.util.Map) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) HandlerMapping(org.springframework.web.reactive.HandlerMapping) ResponseCookie(org.springframework.http.ResponseCookie) SimpleUrlHandlerMapping(org.springframework.web.reactive.handler.SimpleUrlHandlerMapping) HttpHeaders(org.springframework.http.HttpHeaders) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

WebSocketClient (org.springframework.web.reactive.socket.client.WebSocketClient)3 Duration (java.time.Duration)2 Map (java.util.Map)2 ReactorNettyWebSocketClient (org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient)2 HttpServer (org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer)2 TomcatHttpServer (org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer)2 Mono (reactor.core.publisher.Mono)2 URI (java.net.URI)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 WsContextListener (org.apache.tomcat.websocket.server.WsContextListener)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 HttpHeaders (org.springframework.http.HttpHeaders)1