io.vertx.core.http.HttpClient.websocketStream()方法的使用及代码示例

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

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

HttpClient.websocketStream介绍

[英]Create a WebSocket stream to the specified port, host and relative request URI
[中]创建指向指定端口、主机和相对请求URI的WebSocket流

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Server specifies cert that the client trusts (not trust all)
public void testTLSClientTrustServerCertWithSNI() throws Exception {
 testTLS(Cert.NONE, Trust.SNI_JKS_HOST2, Cert.SNI_JKS, Trust.NONE, false, false, false, false, true, true, true, true, new String[0],
   client -> client.websocketStream(4043, "host2.com", "/"));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testReportProtocolViolationOnClient() {
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
  NetSocket sock = getUpgradedNetSocket(req, "/some/path");
  // Let's write an invalid frame
  Buffer buff = Buffer.buffer();
  buff.appendByte((byte)(0x8)).appendByte((byte)0); // Violates protocol with V13 (final control frame)
  sock.write(buff);
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/some/path", null, WebsocketVersion.V13).
   handler(ws -> {
    AtomicReference<Throwable> failure = new AtomicReference<>();
    ws.closeHandler(v -> {
     assertNotNull(failure.get());
     testComplete();
    });
    ws.exceptionHandler(failure::set);
   });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testInvalidSubProtocol(WebsocketVersion version) throws Exception {
 String path = "/some/path";
 String subProtocol = "myprotocol";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols("invalid")).websocketHandler(ws -> {
 });
 server.listen(onSuccess(ar -> {
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol).
    exceptionHandler(t -> {
     // Should fail
     testComplete();
    }).
    handler(ws -> {
    });
 }));
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testCloseCallHandlers(boolean local) {
 String path = "/some/path";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> {
  if (!local) {
   ws.close();
  }
 });
 AtomicInteger doneCount = new AtomicInteger();
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null).
   endHandler(done -> doneCount.incrementAndGet()).
   handler(ws -> {
    assertEquals(0, doneCount.get());
    boolean[] closed = new boolean[1];
    ws.closeHandler(v -> {
     closed[0] = true;
     assertEquals(1, doneCount.get());
     testComplete();
    });
    if (local) {
     vertx.runOnContext(v -> {
      ws.close();
     });
    }
   });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

server.listen(ar -> {
 assertTrue(ar.succeeded());
 client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null).
  handler(ws -> {
   Buffer buff = Buffer.buffer();

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Let's manually handle the websocket handshake and write a frame to the client
public void testHandleWSManually() throws Exception {
 String path = "/some/path";
 String message = "here is some text data";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
  NetSocket sock = getUpgradedNetSocket(req, path);
  // Let's write a Text frame raw
  Buffer buff = Buffer.buffer();
  buff.appendByte((byte)129); // Text frame
  buff.appendByte((byte)message.length());
  buff.appendString(message);
  sock.write(buff);
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path).
    handler(ws -> {
     ws.handler(buff -> {
      assertEquals(message, buff.toString("UTF-8"));
      testComplete();
     });
    });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Client trusts all server certs
public void testClearClientRequestOptionsSetSSL() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, true, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Client trusts all server certs
public void testSSLClientRequestOptionsSetSSL() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, true, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Client trusts all server certs
public void testClearClientRequestOptionsSetClear() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, false, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
// Client trusts all server certs
public void testSSLClientRequestOptionsSetClear() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, false, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testRaceConditionWithWebsocketClientWorker2() throws Exception {
 int size = getOptions().getWorkerPoolSize() - 4;
 List<Context> workers = createWorkers(size + 1);
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT));
 server.websocketHandler(ws -> {
  ws.write(Buffer.buffer("hello"));
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  workers.get(0).runOnContext(v -> {
   ReadStream<WebSocket> webSocketStream = client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/");
   webSocketStream.handler(ws -> {
    ws.handler(buf -> {
     assertEquals("hello", buf.toString());
     testComplete();
    });
   });
  });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testClearClientHandlersOnEnd() {
 String path = "/some/path";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(WebSocketBase::close);
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null).
    handler(ws -> {
     ws.endHandler(v -> {
      try {
       ws.endHandler(null);
       ws.exceptionHandler(null);
       ws.handler(null);
      } catch (Exception e) {
       fail("Was expecting to set to null the handlers when the socket is closed");
       return;
      }
      testComplete();
     });
    });
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testReject(WebsocketVersion version, Integer rejectionStatus, int expectedRejectionStatus) throws Exception {
 String path = "/some/path";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> {
  assertEquals(path, ws.path());
  if (rejectionStatus != null) {
   ws.reject(rejectionStatus);
  } else {
   ws.reject();
  }
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version).
    exceptionHandler(t -> {
     assertTrue(t instanceof WebsocketRejectedException);
     assertEquals(expectedRejectionStatus, ((WebsocketRejectedException)t).getStatus());
     testComplete();
    }).
    handler(ws -> fail("Should not be called"));
 });
 await();
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testTLS(Cert<?> clientCert, Trust<?> clientTrust,
           Cert<?> serverCert, Trust<?> serverTrust,
           boolean requireClientAuth, boolean serverUsesCrl, boolean clientTrustAll,
           boolean clientUsesCrl, boolean shouldPass,
           boolean clientSsl,
           boolean serverSsl,
           boolean sni,
           String[] enabledCipherSuites,
           Function<HttpClient, ReadStream<WebSocket>> wsProvider) throws Exception {
 HttpClientOptions options = new HttpClientOptions();
 options.setSsl(clientSsl);
 options.setTrustAll(clientTrustAll);
 if (clientUsesCrl) {
  options.addCrlPath("tls/root-ca/crl.pem");
 }
 options.setTrustOptions(clientTrust.get());
 options.setKeyCertOptions(clientCert.get());
 for (String suite: enabledCipherSuites) {
  options.addEnabledCipherSuite(suite);
 }
 client = vertx.createHttpClient(options);
 HttpServerOptions serverOptions = new HttpServerOptions();
 serverOptions.setSsl(serverSsl);
 serverOptions.setSni(sni);
 serverOptions.setTrustOptions(serverTrust.get());
 serverOptions.setKeyCertOptions(serverCert.get());
 if (requireClientAuth) {
  serverOptions.setClientAuth(ClientAuth.REQUIRED);
 }
 if (serverUsesCrl) {

代码示例来源:origin: io.vertx/vertx-core

@Test
// Server specifies cert that the client trusts (not trust all)
public void testTLSClientTrustServerCertWithSNI() throws Exception {
 testTLS(Cert.NONE, Trust.SNI_JKS_HOST2, Cert.SNI_JKS, Trust.NONE, false, false, false, false, true, true, true, true, new String[0],
   client -> client.websocketStream(4043, "host2.com", "/"));
}

代码示例来源:origin: io.vertx/vertx-core

private void testInvalidSubProtocol(WebsocketVersion version) throws Exception {
 String path = "/some/path";
 String subProtocol = "myprotocol";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols("invalid")).websocketHandler(ws -> {
 });
 server.listen(onSuccess(ar -> {
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, subProtocol).
    exceptionHandler(t -> {
     // Should fail
     testComplete();
    }).
    handler(ws -> {
    });
 }));
 await();
}

代码示例来源:origin: io.vertx/vertx-core

@Test
// Client trusts all server certs
public void testSSLClientRequestOptionsSetClear() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, false, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: io.vertx/vertx-core

@Test
// Client trusts all server certs
public void testClearClientRequestOptionsSetSSL() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, true, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: io.vertx/vertx-core

@Test
// Client trusts all server certs
public void testSSLClientRequestOptionsSetSSL() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(true);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, true, true, false, new String[0], client -> client.websocketStream(options));
}

代码示例来源:origin: io.vertx/vertx-core

@Test
// Client trusts all server certs
public void testClearClientRequestOptionsSetClear() throws Exception {
 RequestOptions options = new RequestOptions().setHost(HttpTestBase.DEFAULT_HTTP_HOST).setURI("/").setPort(4043).setSsl(false);
 testTLS(Cert.NONE, Trust.NONE, Cert.SERVER_JKS, Trust.NONE, false, false, true, false, true, false, false, false, new String[0], client -> client.websocketStream(options));
}

相关文章