当前位置: 首页>>代码示例>>Java>>正文


Java WebSocket类代码示例

本文整理汇总了Java中io.vertx.core.http.WebSocket的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


WebSocket类属于io.vertx.core.http包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testConnection

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testConnection() {
  AtomicReference<Throwable> error = new AtomicReference<>();
  AtomicReference<Buffer> frame = new AtomicReference<>();
  AtomicReference<WebSocket> socket = new AtomicReference<>();

  vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
      ("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
    socket.set(ws);
    ws.exceptionHandler(error::set)
        .handler(frame::set)
        .write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
            "heart-beat", "10000,10000"), null).toBuffer());
  });

  await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && frame.get() != null);
  assertThat(frame.get().toString()).startsWith("CONNECTED")
      .contains("server:vertx-stomp", "heart-beat:", "session:", "version:1.2");
  socket.get().close();
}
 
开发者ID:vert-x3,项目名称:vertx-stomp,代码行数:21,代码来源:WebSocketBridgeTest.java

示例2: testTextFrameRawWebSocket

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testTextFrameRawWebSocket() throws InterruptedException {
  String serverPath = "/textecho";
  setupSockJsServer(serverPath, this::echoRequest);

  String message = "hello";
  AtomicReference<String> receivedReply = new AtomicReference<>();
  WebSocket ws = setupRawWebsocketClient(serverPath);

  ws.handler(replyBuffer -> receivedReply.set(replyBuffer.toString()));

  ws.writeFrame(WebSocketFrame.textFrame(message, true));

  await(5, TimeUnit.SECONDS);

  assertEquals("Client reply should have matched request", message, receivedReply.get());
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:18,代码来源:SockJSHandlerTest.java

示例3: testTextFrameSockJs

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testTextFrameSockJs() throws InterruptedException {
  String serverPath = "/text-sockjs";
  setupSockJsServer(serverPath, this::echoRequest);

  List<Buffer> receivedMessages = new ArrayList<>();
  WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
  String messageToSend = "[\"testMessage\"]";
  openedWebSocket.writeFrame(WebSocketFrame.textFrame(messageToSend, true));

  await(5, TimeUnit.SECONDS);

  assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
  Buffer expectedReply = Buffer.buffer("a" + messageToSend);
  assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
  assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:18,代码来源:SockJSHandlerTest.java

示例4: setupSockJsClient

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
 * This sets up a handler on the websocket
 */
private WebSocket setupSockJsClient(String serverPath, List<Buffer> receivedMessagesCollector)
        throws InterruptedException
{
  String requestURI = serverPath + "/000/000/websocket";

  AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>();
  CountDownLatch openSocketCountDown = new CountDownLatch(1);
  client.websocket(requestURI, ws -> {
    openedWebSocketReference.set(ws);
    ws.handler(replyBuffer -> {
      log.debug("Client received " + replyBuffer);
      String textReply = replyBuffer.toString();
      if ("o".equals(textReply)) {
        openSocketCountDown.countDown();
      } else {
        receivedMessagesCollector.add(replyBuffer);
      }
    });
    ws.endHandler(v -> testComplete());
    ws.exceptionHandler(this::fail);
  });

  openSocketCountDown.await(5, TimeUnit.SECONDS);
  return openedWebSocketReference.get();
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:29,代码来源:SockJSHandlerTest.java

示例5: setupRawWebsocketClient

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
 * This does not set up a handler on the websocket
 */
private WebSocket setupRawWebsocketClient(String serverPath)
        throws InterruptedException
{
  String requestURI = serverPath + "/websocket";

  AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>();
  CountDownLatch openSocketCountDown = new CountDownLatch(1);
  client.websocket(requestURI, ws -> {
    openedWebSocketReference.set(ws);
    openSocketCountDown.countDown();
    ws.endHandler(v -> testComplete());
    ws.exceptionHandler(this::fail);
  });

  openSocketCountDown.await(5, TimeUnit.SECONDS);
  return openedWebSocketReference.get();
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:21,代码来源:SockJSHandlerTest.java

示例6: sendHeartbeat

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
private void sendHeartbeat(WebSocket ws) {
  try {
    ws.writeFrame(new WebSocketFrameImpl(FrameType.PING));
  } catch (IllegalStateException e) {
    LOGGER.error("heartbeat fail", e);
  }
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:8,代码来源:ConfigCenterClient.java

示例7: connected

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public Map<HttpClientMetrics, ?> connected(Map<HttpClientMetrics, ?> endpointMetric,
                                           Map<HttpClientMetrics, ?> socketMetric,
                                           WebSocket webSocket) {
    return unmap2WithResult(endpointMetric, socketMetric,
            (m, ec, sc) -> m.connected(ec, sc, webSocket));
}
 
开发者ID:unbroken-dome,项目名称:vertx-spring,代码行数:8,代码来源:DispatchingHttpClientMetrics.java

示例8: connected

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public StopWatch connected(StopWatch endpointWatch, StopWatch socketWatch, WebSocket webSocket) {
    counterService.increment("websockets.connected");
    StopWatch websocketWatch = new StopWatch();
    websocketWatch.start("websocket");
    return websocketWatch;
}
 
开发者ID:unbroken-dome,项目名称:vertx-spring,代码行数:8,代码来源:VertxActuatorMetrics.java

示例9: connected

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public SocketAddress connected(Void aVoid, SocketAddress socketAddress, WebSocket webSocket) {
  long value = wsConnections.incrementAndGet();
  setMetric(SENSISION_CLASS_WEBSOCKETS, defaultLabels, value);
  incrementMetric(SENSISION_CLASS_WEBSOCKET_CONNECTED_COUNT, defaultLabels);

  return null;
}
 
开发者ID:slambour,项目名称:vertx-warp10-metrics,代码行数:9,代码来源:HttpClientMetricsImpl.java

示例10: testSendingAMessage

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testSendingAMessage() {
  AtomicReference<Throwable> error = new AtomicReference<>();
  AtomicReference<Frame> frame = new AtomicReference<>();
  AtomicReference<WebSocket> socket = new AtomicReference<>();

  AtomicReference<StompClientConnection> client = new AtomicReference<>();

  clients.add(StompClient.create(vertx).connect(61613, "localhost", connection -> {
    connection.result().subscribe("foo", frame::set, r -> {
      client.set(connection.result());
    });
  }));

  await().atMost(10, TimeUnit.SECONDS).until(() -> client.get() != null);
  await().atMost(10, TimeUnit.SECONDS).until(() -> server.stompHandler().getDestination("foo") != null);


  vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
      ("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
    socket.set(ws);
    ws.exceptionHandler(error::set)
        .handler(buffer -> {
          if (buffer.toString().startsWith("CONNECTED")) {
            ws.write(
                new Frame(Frame.Command.SEND, Headers.create("header", "value", "destination", "foo"), Buffer
                    .buffer("hello")).toBuffer());
          }
        })
        .write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
            "heart-beat", "10000,10000"), null).toBuffer());
  });

  await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && frame.get() != null);
  assertThat(frame.get().toString()).startsWith("MESSAGE")
      .contains("destination:foo", "header:value", "\nhello");
  socket.get().close();
}
 
开发者ID:vert-x3,项目名称:vertx-stomp,代码行数:39,代码来源:WebSocketBridgeTest.java

示例11: testPingFromServer

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testPingFromServer() {
  AtomicReference<Throwable> error = new AtomicReference<>();
  AtomicReference<WebSocket> socket = new AtomicReference<>();
  AtomicReference<Boolean> flag = new AtomicReference<>();

  AtomicReference<StompClientConnection> client = new AtomicReference<>();

  clients.add(StompClient.create(vertx).connect(61613, "localhost", connection -> {
    client.set(connection.result());
  }));

  await().atMost(10, TimeUnit.SECONDS).until(() -> client.get() != null);

  vertx.createHttpClient().websocket(8080, "localhost", "/stomp", MultiMap.caseInsensitiveMultiMap().add
          ("Sec-WebSocket-Protocol", "v10.stomp, v11.stomp, v12.stomp"), ws -> {
    socket.set(ws);
    ws.exceptionHandler(error::set)
            .handler(buffer -> {
              vertx.setTimer(1000, id -> {
                flag.set(true);
              });
            })
            .write(new Frame(Frame.Command.CONNECT, Headers.create("accept-version", "1.2,1.1,1.0",
                    "heart-beat", "100,0"), null).toBuffer());
  });

  await().atMost(10, TimeUnit.SECONDS).until(() -> error.get() == null && flag.get() != null);
  socket.get().close();
}
 
开发者ID:vert-x3,项目名称:vertx-stomp,代码行数:31,代码来源:WebSocketBridgeTest.java

示例12: sendTypeToBridge

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
protected static void sendTypeToBridge(WebSocket ws, String type, String address, String msg) {
  JsonObject json = new JsonObject();
  json.put("type", type);
  json.put("address", address);
  json.put("body", msg);
  ws.write(Buffer.buffer(json.toString()));
}
 
开发者ID:aesteve,项目名称:nubes,代码行数:8,代码来源:EventBusBridgeTestBase.java

示例13: connected

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Override
public SocketAddress connected(Void endpointMetric, SocketAddress key, WebSocket webSocket) {
  HttpClientConnectionsMeasurements measurements = connectionsMeasurements.get(key);
  if (measurements != null) {
    measurements.incrementWsConnectionCount();
  }
  return key;
}
 
开发者ID:vert-x3,项目名称:vertx-hawkular-metrics,代码行数:9,代码来源:HttpClientMetricsImpl.java

示例14: testCombineBinaryContinuationFramesRawWebSocket

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
/**
 * Writing multiple continuation frames from the client side should result in a single message on the server side
 * after the frames are re-combined
 */
@Test
public void testCombineBinaryContinuationFramesRawWebSocket() throws InterruptedException {
  String serverPath = "/combine";

  AtomicReference<Buffer> serverReceivedMessage = new AtomicReference<>();
  setupSockJsServer(serverPath, (sock, requestBuffer) -> {
    serverReceivedMessage.set(requestBuffer);
    sock.write(Buffer.buffer("reply"));
    sock.close();
  });


  Buffer largeMessage = Buffer.buffer(TestUtils.randomAlphaString(30));
  WebSocketFrame frame1 = WebSocketFrame.binaryFrame(largeMessage.slice(0, 10), false);
  WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
  WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);

  WebSocket ws = setupRawWebsocketClient(serverPath);
  ws.writeFrame(frame1);
  ws.writeFrame(frame2);
  ws.writeFrame(frame3);

  await(5, TimeUnit.SECONDS);

  assertEquals("Server did not combine continuation frames correctly", largeMessage, serverReceivedMessage.get());
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:31,代码来源:SockJSHandlerTest.java

示例15: testSplitLargeReplyRawWebSocket

import io.vertx.core.http.WebSocket; //导入依赖的package包/类
@Test
public void testSplitLargeReplyRawWebSocket() throws InterruptedException {
  String serverPath = "/split";

  String largeReply = TestUtils.randomAlphaString(65536 * 5);
  Buffer largeReplyBuffer = Buffer.buffer(largeReply);

  setupSockJsServer(serverPath, (sock, requestBuffer) -> {
    sock.write(largeReplyBuffer);
    sock.close();
  });

  Buffer totalReplyBuffer = Buffer.buffer(largeReplyBuffer.length());
  AtomicInteger receivedReplies = new AtomicInteger(0);
  WebSocket ws = setupRawWebsocketClient(serverPath);
  ws.handler(replyBuffer -> {
            totalReplyBuffer.appendBuffer(replyBuffer);
            receivedReplies.incrementAndGet();

          });

  ws.writeFrame(WebSocketFrame.binaryFrame(Buffer.buffer("hello"), true));

  await(5, TimeUnit.SECONDS);

  int receivedReplyCount = receivedReplies.get();
  assertEquals("Combined reply on client should equal message from server", largeReplyBuffer, totalReplyBuffer);
  assertTrue("Should have received > 1 reply frame, actually received " + receivedReplyCount, receivedReplyCount > 1);
}
 
开发者ID:vert-x3,项目名称:vertx-web,代码行数:30,代码来源:SockJSHandlerTest.java


注:本文中的io.vertx.core.http.WebSocket类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。