received.clear();
latch = new CountDownLatch(1);
AsyncHttpClient c = new AsyncHttpClient();
WebSocket websocket = c.prepareGet("ws://127.0.0.1:" + 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 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();
websocket.sendTextMessage("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 AsyncHttpClient();
websocket = c.prepareGet("ws://127.0.0.1:" + 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 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();
websocket.sendTextMessage("wine");
assertTrue(latch.await(10, TimeUnit.SECONDS));
assertEquals(1, received.size());
assertEquals("The pub has wine", received.get(0));
websocket.close();
c.close();
}