Search in sources :

Example 1 with DefaultWebSocketListener

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

the class WebsocketProducerRouteExampleTest method testWSBytesHttpCall.

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

        @Override
        public void onMessage(byte[] message) {
            received.add(message);
            log.info("received --> " + Arrays.toString(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
    byte[] testmessage = "Beer on stock at Apache Mall".getBytes("utf-8");
    producer.sendBodyAndHeader(testmessage, WebsocketConstants.SEND_TO_ALL, "true");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertEquals(1, received.size());
    Object r = received.get(0);
    assertTrue(r instanceof byte[]);
    assertArrayEquals(testmessage, (byte[]) r);
    websocket.close();
    c.close();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) DefaultWebSocketListener(org.asynchttpclient.ws.DefaultWebSocketListener) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) DefaultAsyncHttpClient(org.asynchttpclient.DefaultAsyncHttpClient) WebSocket(org.asynchttpclient.ws.WebSocket) Test(org.junit.Test)

Aggregations

RouteBuilder (org.apache.camel.builder.RouteBuilder)1 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)1 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)1 DefaultWebSocketListener (org.asynchttpclient.ws.DefaultWebSocketListener)1 WebSocket (org.asynchttpclient.ws.WebSocket)1 Test (org.junit.Test)1