reactor.netty.http.client.HttpClient.websocket()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.0k)|赞(0)|评价(0)|浏览(196)

本文整理了Java中reactor.netty.http.client.HttpClient.websocket()方法的一些代码示例,展示了HttpClient.websocket()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpClient.websocket()方法的具体详情如下:
包路径:reactor.netty.http.client.HttpClient
类名称:HttpClient
方法名:websocket

HttpClient.websocket介绍

[英]HTTP Websocket to connect the HttpClient.
[中]

代码示例

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Void> execute(URI url, HttpHeaders requestHeaders, WebSocketHandler handler) {
  return getHttpClient()
      .headers(nettyHeaders -> setNettyHeaders(requestHeaders, nettyHeaders))
      .websocket(StringUtils.collectionToCommaDelimitedString(handler.getSubProtocols()))
      .uri(url.toString())
      .handle((inbound, outbound) -> {
        HttpHeaders responseHeaders = toHttpHeaders(inbound);
        String protocol = responseHeaders.getFirst("Sec-WebSocket-Protocol");
        HandshakeInfo info = new HandshakeInfo(url, responseHeaders, Mono.empty(), protocol);
        NettyDataBufferFactory factory = new NettyDataBufferFactory(outbound.alloc());
        WebSocketSession session = new ReactorNettyWebSocketSession(inbound, outbound, info, factory);
        if (logger.isDebugEnabled()) {
          logger.debug("Started session '" + session.getId() + "' for " + url);
        }
        return handler.handle(session);
      })
      .doOnRequest(n -> {
        if (logger.isDebugEnabled()) {
          logger.debug("Connecting to " + url);
        }
      })
      .next();
}

代码示例来源:origin: rsocket/rsocket-java

@Override
public Mono<DuplexConnection> connect() {
 return client
   .headers(headers -> transportHeaders.get().forEach(headers::set))
   .websocket()
   .uri(path)
   .connect()
   .map(WebsocketDuplexConnection::new);
}

代码示例来源:origin: reactor/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @param maxFramePayloadLength maximum allowable frame payload length
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket(int maxFramePayloadLength) {
  return websocket("", maxFramePayloadLength);
}

代码示例来源:origin: reactor/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket() {
  return websocket("");
}

代码示例来源:origin: reactor/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @param subprotocols a websocket subprotocol comma separated list
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket(String subprotocols) {
  return websocket(subprotocols, 65536);
}

代码示例来源:origin: io.projectreactor.netty/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket() {
  return websocket("");
}

代码示例来源:origin: io.projectreactor.netty/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @param subprotocols a websocket subprotocol comma separated list
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket(String subprotocols) {
  return websocket(subprotocols, 65536);
}

代码示例来源:origin: io.projectreactor.netty/reactor-netty

/**
 * HTTP Websocket to connect the {@link HttpClient}.
 *
 * @param maxFramePayloadLength maximum allowable frame payload length
 *
 * @return a {@link WebsocketSender} ready to consume for response
 */
public final WebsocketSender websocket(int maxFramePayloadLength) {
  return websocket("", maxFramePayloadLength);
}

代码示例来源:origin: io.rsocket/rsocket-transport-netty

@Override
public Mono<DuplexConnection> connect() {
 return client
   .headers(headers -> transportHeaders.get().forEach(headers::set))
   .websocket()
   .uri(path)
   .connect()
   .map(WebsocketDuplexConnection::new);
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void anySubprotocolSelectsFirstClientProvided() {
  httpServer = HttpServer.create()
              .port(0)
              .handle((in, out) -> out.sendWebsocket("proto2,*", (i, o) -> o.sendString(
                  Mono.just("SERVER:" + o.selectedSubprotocol()))))
              .wiretap(true)
              .bindNow();
  String res = Objects.requireNonNull(
      HttpClient.create()
           .port(httpServer.address().getPort())
           .headers(h -> h.add("Authorization", auth))
           .websocket("proto1, proto2")
           .uri("/test")
           .handle((in, out) -> in.receive()
                       .asString()
                       .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv))
           .log()
           .collectList()
           .block(Duration.ofSeconds(30))).get(0);
  Assert.assertThat(res, is("CLIENT:proto1-SERVER:proto1"));
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void noSubprotocolSelected() {
  httpServer = HttpServer.create()
              .port(0)
              .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(
                  Mono.just("SERVER:" + o.selectedSubprotocol()))))
              .wiretap(true)
              .bindNow();
  String res = Objects.requireNonNull(
      HttpClient.create()
           .port(httpServer.address().getPort())
           .headers(h -> h.add("Authorization", auth))
           .websocket()
           .uri("/test")
           .handle((in, out) -> in.receive()
                       .asString()
                       .map(srv -> "CLIENT:" + in.selectedSubprotocol() + "-" + srv))
           .log()
           .collectList()
           .block(Duration.ofSeconds(30))).get(0);
  Assert.assertThat(res, is("CLIENT:null-SERVER:null"));
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void simpleTest() {
  httpServer = HttpServer.create()
              .port(0)
              .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(Mono.just("test"))))
              .wiretap(true)
              .bindNow();
  String res = Objects.requireNonNull(
      HttpClient.create()
           .port(httpServer.address().getPort())
           .wiretap(true)
           .headers(h -> h.add("Authorization", auth))
           .websocket()
           .uri("/test")
           .handle((i, o) -> i.receive().asString())
           .log("client")
           .collectList()
           .block()).get(0);
  Assert.assertThat(res, is("test"));
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void testMaxFramePayloadLengthSuccess() {
  httpServer = HttpServer.create()
      .port(0)
      .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(Mono.just("12345678901"))))
      .wiretap(true)
      .bindNow();
  Mono<Void> response = HttpClient.create()
      .port(httpServer.address().getPort())
      .websocket(11)
      .handle((in, out) -> in.receive()
          .asString()
          .map(srv -> srv))
      .log()
      .then();
  StepVerifier.create(response)
      .expectComplete()
      .verify(Duration.ofSeconds(30));
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void testMaxFramePayloadLengthFailed() {
  httpServer = HttpServer.create()
      .port(0)
      .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(Mono.just("12345678901"))))
      .wiretap(true)
      .bindNow();
  Mono<Void> response = HttpClient.create()
          .port(httpServer.address().getPort())
          .websocket(10)
          .handle((in, out) -> in.receive()
              .asString()
              .map(srv -> srv))
          .log()
          .then();
  StepVerifier.create(response)
      .expectError(CorruptedFrameException.class)
      .verify(Duration.ofSeconds(30));
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void simpleSubprotocolServerNoSubprotocol() {
  httpServer = HttpServer.create()
              .port(0)
              .handle((in, out) -> out.sendWebsocket((i, o) -> o.sendString(
                        Mono.just("test"))))
              .wiretap(true)
              .bindNow();
  StepVerifier.create(
      HttpClient.create()
           .port(httpServer.address().getPort())
           .headers(h -> h.add("Authorization", auth))
           .websocket("SUBPROTOCOL,OTHER")
           .uri("/test")
           .handle((i, o) -> i.receive().asString())
  )
        .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER");
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void simpleSubprotocolServerNotSupported() {
  httpServer = HttpServer.create()
              .port(0)
              .handle((in, out) -> out.sendWebsocket(
                  "protoA,protoB",
                  (i, o) -> {
                    return o.sendString(Mono.just("test"));
                  }))
              .wiretap(true)
              .bindNow();
  StepVerifier.create(
      HttpClient.create()
           .port(httpServer.address().getPort())
           .headers(h -> h.add("Authorization", auth))
           .websocket("SUBPROTOCOL,OTHER")
           .uri("/test")
           .handle((i, o) -> i.receive().asString())
  )
        //the SERVER returned null which means that it couldn't select a protocol
        .verifyErrorMessage("Invalid subprotocol. Actual: null. Expected one of: SUBPROTOCOL,OTHER");
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void testIssue460() {
  DisposableServer server =
      HttpServer.create()
           .port(0)
           .host("::1")
           .wiretap(true)
           .handle((req, res) -> res.sendWebsocket((in, out) -> Mono.never()))
           .bindNow();
  HttpClient httpClient =
      HttpClient.create()
           .addressSupplier(server::address)
           .wiretap(true)
           .headers(h -> h.add(HttpHeaderNames.HOST, "[::1"));
  StepVerifier.create(httpClient.websocket()
                 .connect())
                 .expectError()
                 .verify(Duration.ofSeconds(30));
  server.disposeNow();
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void testIssue473() throws Exception {
  SelfSignedCertificate cert = new SelfSignedCertificate();
  SslContextBuilder serverSslContextBuilder =
      SslContextBuilder.forServer(cert.certificate(), cert.privateKey());
  DisposableServer server =
      HttpServer.create()
           .port(0)
           .wiretap(true)
           .secure(spec -> spec.sslContext(serverSslContextBuilder))
           .bindNow();
  StepVerifier.create(
      HttpClient.create(ConnectionProvider.newConnection())
           .secure()
           .websocket()
           .uri("wss://" + server.host() + ":" + server.port())
           .handle((in, out) -> Mono.empty()))
        .expectErrorMatches(t -> t.getCause() instanceof CertificateException)
      .verify(Duration.ofSeconds(30));
  server.disposeNow();
}

代码示例来源:origin: reactor/reactor-netty

@Test
public void testCloseWebSocketFrameSentByClient() {
  httpServer =
      HttpServer.create()
           .port(0)
           .handle((req, res) ->
               res.sendWebsocket((in, out) -> out.sendString(Mono.just("echo"))
                                .sendObject(new CloseWebSocketFrame())))
           .wiretap(true)
           .bindNow();
  Mono<Void> response =
      HttpClient.create()
           .port(httpServer.address().getPort())
           .websocket()
           .uri("/")
           .handle((in, out) -> out.sendObject(in.receiveFrames()
                              .doOnNext(WebSocketFrame::retain)
                              .then()))
           .next();
  StepVerifier.create(response)
        .expectComplete()
        .verify(Duration.ofSeconds(30));
}

代码示例来源:origin: reactor/reactor-netty

private void doTestIssue444(BiFunction<WebsocketInbound, WebsocketOutbound, Publisher<Void>> fn) {
  httpServer =
      HttpServer.create()
           .host("localhost")
           .port(0)
           .handle((req, res) -> res.sendWebsocket(null, fn))
           .wiretap(true)
           .bindNow();
  StepVerifier.create(
      HttpClient.create()
           .addressSupplier(() -> httpServer.address())
           .wiretap(true)
           .websocket()
           .uri("/")
           .handle((i, o) -> i.receiveFrames()
                     .then()))
        .expectComplete()
        .verify(Duration.ofSeconds(30));
}

相关文章