예제 #1
0
  @Test
  public void testEchoReturn() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("empty-web.xml");
    wsb.copyClass(EchoReturnEndpoint.class);

    try {
      wsb.start();
      URI uri = wsb.getServerBaseURI();

      WebAppContext webapp = wsb.createWebAppContext();
      wsb.deployWebapp(webapp);
      wsb.dump();

      WebSocketClient client = new WebSocketClient();
      try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        Future<Session> future = client.connect(clientEcho, uri.resolve("echoreturn"));
        // wait for connect
        future.get(1, TimeUnit.SECONDS);
        clientEcho.sendMessage("Hello World");
        Queue<String> msgs = clientEcho.awaitMessages(1);
        Assert.assertEquals("Expected message", "Hello World", msgs.poll());
      } finally {
        client.stop();
      }
    } finally {
      wsb.stop();
    }
  }
  private ServiceSocket getConnectionSocket() throws URISyntaxException, Exception {
    URI uri = getUri();

    String connectionId = getThreadName() + getConnectionId();
    ServiceSocket socket;
    WebSocketClient webSocketClient;
    if (isStreamingConnection()) {
      if (connectionList.containsKey(connectionId)) {
        socket = connectionList.get(connectionId);
        socket.initialize();
        return socket;
      } else {
        socket = new ServiceSocket(this);
        connectionList.put(connectionId, socket);
      }
    } else {
      socket = new ServiceSocket(this);
    }

    SslContextFactory sslContexFactory = new SslContextFactory();
    sslContexFactory.setTrustAll(isIgnoreSslErrors());
    webSocketClient = new WebSocketClient(sslContexFactory);

    webSocketClient.start();
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    webSocketClient.connect(socket, uri, request);

    int connectionTimeout = Integer.parseInt(getConnectionTimeout());
    socket.awaitOpen(connectionTimeout, TimeUnit.MILLISECONDS);

    return socket;
  }
예제 #3
0
  protected void connect(URI uri, long timeout, JettyConnection connection) throws Exception {

    if (!getWebsocketClient().isStarted()) websocketClient.start();

    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(WampFactory.getProtocolName());

    Future<Session> future = websocketClient.connect(connection, uri, request);

    if (timeout > 0) future.get(timeout, TimeUnit.MILLISECONDS);
    else future.get();
  }
예제 #4
0
 public void setUpConnection(String playerDest, String publDest) {
   try {
     publClient.start();
     playerClient.start();
     URI publURI = new URI(publDest);
     URI playerURI = new URI(playerDest);
     ClientUpgradeRequest publRequest = new ClientUpgradeRequest();
     ClientUpgradeRequest playerRequest = new ClientUpgradeRequest();
     publClient.connect(publSocket, publURI, publRequest);
     playerClient.connect(playerSocket, playerURI, playerRequest);
   } catch (Throwable t) {
     t.printStackTrace();
   }
 }
예제 #5
0
  public Session connect(AgentWebSocketClientController controller) throws Exception {
    if (webSocketClient == null || !webSocketClient.isRunning()) {
      if (webSocketClient != null) {
        webSocketClient.stop();
      }
      webSocketClient = builder.build();
      webSocketClient.start();
    }

    LOG.info("Connecting to websocket endpoint: " + urlService.getAgentRemoteWebSocketUrl());
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.addExtensions("fragment;maxLength=" + getMessageBufferSize());
    return webSocketClient
        .connect(controller, new URI(urlService.getAgentRemoteWebSocketUrl()), request)
        .get();
  }
예제 #6
0
  @Test
  public void testBatchModeAuto() throws Exception {
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());

    final CountDownLatch latch = new CountDownLatch(1);
    WebSocketAdapter adapter =
        new WebSocketAdapter() {
          @Override
          public void onWebSocketText(String message) {
            latch.countDown();
          }
        };
    try (Session session = client.connect(adapter, uri).get()) {
      RemoteEndpoint remote = session.getRemote();

      Future<Void> future = remote.sendStringByFuture("batch_mode_on");
      // The write is aggregated and therefore completes immediately.
      future.get(1, TimeUnit.MICROSECONDS);

      // Wait for the echo.
      Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
  }
예제 #7
0
 private int getMessageBufferSize() {
   return webSocketClient.getPolicy().getMaxBinaryMessageBufferSize();
 }
예제 #8
0
 public void stopWebsocketClient() throws Exception {
   if (websocketClient != null) websocketClient.stop();
 }