Search in sources :

Example 1 with WebSocketTextListener

use of org.asynchttpclient.ws.WebSocketTextListener in project camel by apache.

the class WebsocketProducerRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/shop").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    // Send message to the direct endpoint
    producer.sendBodyAndHeader("Beer on stock at Apache Mall", WebsocketConstants.SEND_TO_ALL, "true");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    Object r = received.get(0);
    assertTrue(r instanceof String);
    assertEquals("Beer on stock at Apache Mall", r);
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 2 with WebSocketTextListener

use of org.asynchttpclient.ws.WebSocketTextListener in project camel by apache.

the class WebsocketSSLRouteExampleTest method testWSHttpCall.

@Test
public void testWSHttpCall() throws Exception {
    AsyncHttpClient c = createAsyncHttpSSLClient();
    WebSocket websocket = c.prepareGet("wss://127.0.0.1:" + port + "/test").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    getMockEndpoint("mock:client").expectedBodiesReceived("Hello from WS client");
    websocket.sendMessage("Hello from WS client");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertMockEndpointsSatisfied();
    assertEquals(10, received.size());
    for (int i = 0; i < 10; i++) {
        assertEquals(">> Welcome on board!", received.get(i));
    }
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 3 with WebSocketTextListener

use of org.asynchttpclient.ws.WebSocketTextListener in project camel by apache.

the class WebsocketTwoRoutesExampleTest method testWSHttpCallEcho.

@Test
public void testWSHttpCallEcho() throws Exception {
    // We call the route WebSocket BAR
    received.clear();
    latch = new CountDownLatch(1);
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/bar").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("The bar has Beer", received.get(0));
    websocket.close();
    c.close();
    // We call the route WebSocket PUB
    received.clear();
    latch = new CountDownLatch(1);
    c = new DefaultAsyncHttpClient();
    websocket = c.prepareGet("ws://localhost:" + port + "/pub").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("wine");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    assertEquals("The pub has wine", received.get(0));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocketUpgradeHandler(org.asynchttpclient.ws.WebSocketUpgradeHandler) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 4 with WebSocketTextListener

use of org.asynchttpclient.ws.WebSocketTextListener in project camel by apache.

the class WebsocketTwoRoutesToSIndividualAndBroadcastEndpointExampleTest method testWSHttpCallEcho.

@Test
public void testWSHttpCallEcho() throws Exception {
    // We call the route WebSocket BAR
    received.clear();
    latch = new CountDownLatch(2);
    AsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/bar").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(2, received.size());
    //Cannot guarantee the order in which messages are received
    assertTrue(received.contains("The bar has Beer"));
    assertTrue(received.contains("Broadcasting to Bar"));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Example 5 with WebSocketTextListener

use of org.asynchttpclient.ws.WebSocketTextListener in project camel by apache.

the class WebsocketTwoRoutesToSameEndpointExampleTest method testWSHttpCallEcho.

@Test
public void testWSHttpCallEcho() throws Exception {
    // We call the route WebSocket BAR
    received.clear();
    latch = new CountDownLatch(2);
    DefaultAsyncHttpClient c = new DefaultAsyncHttpClient();
    WebSocket websocket = c.prepareGet("ws://localhost:" + port + "/bar").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {

        @Override
        public void onMessage(String message) {
            received.add(message);
            log.info("received --> " + message);
            latch.countDown();
        }

        @Override
        public void onOpen(WebSocket websocket) {
        }

        @Override
        public void onClose(WebSocket websocket) {
        }

        @Override
        public void onError(Throwable t) {
            t.printStackTrace();
        }
    }).build()).get();
    websocket.sendMessage("Beer");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(2, received.size());
    //Cannot guarantee the order in which messages are received
    assertTrue(received.contains("The bar has Beer"));
    assertTrue(received.contains("Broadcasting to Bar"));
    websocket.close();
    c.close();
}
Also used : WebSocketTextListener(org.asynchttpclient.ws.WebSocketTextListener) RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)10 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)10 WebSocket (org.asynchttpclient.ws.WebSocket)10 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)10 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)3 WebSocketUpgradeHandler (org.asynchttpclient.ws.WebSocketUpgradeHandler)1