Example #1
0
 /** Closes the web socket connection */
 public void close() {
   if (webSocket != null) {
     running = false;
     webSocket.close();
     webSocket = null;
   }
 }
Example #2
0
  @Test
  public void happyTest() throws Exception {
    BasicWSRoute route = context.getRegistry().lookup("basicRoute", BasicWSRoute.class);

    AsyncHttpClient c = new AsyncHttpClient();

    WebSocket websocket =
        c.prepareGet("ws:" + route.getConnectionUri())
            .execute(
                new WebSocketUpgradeHandler.Builder()
                    .addWebSocketListener(
                        new WebSocketTextListener() {
                          @Override
                          public void onMessage(String message) {
                            received.add(message);
                            System.out.println("received --> " + message);
                            latch.countDown();
                          }

                          @Override
                          public void onFragment(String fragment, boolean last) {}

                          @Override
                          public void onOpen(WebSocket websocket) {}

                          @Override
                          public void onClose(WebSocket websocket) {}

                          @Override
                          public void onError(Throwable t) {
                            t.printStackTrace();
                          }
                        })
                    .build())
            .get();

    // Jackson
    ObjectMapper mapper = new ObjectMapper();
    WsMessage info = new WsMessage();
    info.setHeader("woot");
    info.setContent("hest");

    websocket.sendTextMessage(mapper.writeValueAsString(info));
    assertTrue(latch.await(10, TimeUnit.SECONDS));

    assertEquals(1, received.size());
    WsMessage result = mapper.readValue(received.get(0), WsMessage.class);
    assertTrue(result.getContent().startsWith("pong:"));

    websocket.close();
    c.close();
  }
  @Test
  public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new AsyncHttpClient();

    WebSocket websocket =
        c.prepareGet("ws://127.0.0.1:" + port + "/echo")
            .execute(
                new WebSocketUpgradeHandler.Builder()
                    .addWebSocketListener(
                        new WebSocketTextListener() {
                          @Override
                          public void onMessage(String message) {}

                          @Override
                          public void onFragment(String fragment, boolean last) {}

                          @Override
                          public void onOpen(WebSocket websocket) {}

                          @Override
                          public void onClose(WebSocket websocket) {}

                          @Override
                          public void onError(Throwable t) {
                            t.printStackTrace();
                          }
                        })
                    .build())
            .get();

    MockEndpoint result = getMockEndpoint("mock:result");
    result.expectedBodiesReceived("Test");

    websocket.sendTextMessage("Test");

    result.assertIsSatisfied();

    websocket.close();
    c.close();
  }
 public void close() {
   websocket.close();
   client.close();
 }