@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendTextMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendTextFrame("TEXT").get(10, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void streamBytesExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendBinaryFrame("STREAM".getBytes(), true, 0).get(1, TimeUnit.SECONDS); } }
@Test public void echoFragments() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() { @Override public void onTextFrame(String payload, boolean finalFragment, int rsv) { text.set(payload); latch.countDown(); } @Override public void onOpen(WebSocket websocket) { } @Override public void onClose(WebSocket websocket, int code, String reason) { latch.countDown(); } @Override public void onError(Throwable t) { t.printStackTrace(); latch.countDown(); } }).build()).get(); websocket.sendTextFrame("ECHO", false, 0); websocket.sendContinuationFrame("ECHO", true, 0); latch.await(); assertEquals(text.get(), "ECHOECHO"); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendPingMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendPingFrame("PING".getBytes()).get(10, TimeUnit.SECONDS); } }
@Test(timeOut = 60000, expectedExceptions = ExecutionException.class) public void sendPongMessageExpectFailure() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { CountDownLatch closeLatch = new CountDownLatch(1); WebSocket websocket = getWebSocket(c, closeLatch); websocket.sendCloseFrame(); closeLatch.await(1, TimeUnit.SECONDS); websocket.sendPongFrame("PONG".getBytes()).get(1, TimeUnit.SECONDS); } }
@Override public void onOpen(WebSocket websocket) { websocket.sendTextFrame("ECHO"); websocket.sendTextFrame("ECHO"); }
public void sendTextMessage(String message) { websocket.sendMessage(message); }
@Override protected void doStop() throws Exception { if (websocket != null && websocket.isOpen()) { if (LOG.isDebugEnabled()) { LOG.debug("Disconnecting from {}", getHttpUri().toASCIIString()); } websocket.removeWebSocketListener(listener); websocket.sendCloseFrame(); websocket = null; } super.doStop(); }
@Override public void onOpen(WebSocket websocket) { websocket.sendBinaryFrame(ECHO_BYTES); websocket.sendBinaryFrame(ECHO_BYTES); }
@Test(timeOut = 60000) public void onCloseWithCode() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get(); websocket.sendCloseFrame(); latch.await(); assertTrue(text.get().startsWith("1000"), "Expected a 1000 code but got " + text.get()); } }
public void close() { logger.info("Slack RTM closing..."); stop = true; if (webSocket != null && webSocket.isOpen()) { webSocket.sendCloseFrame(); } if (asyncHttpClient != null && !asyncHttpClient.isClosed()) { try { asyncHttpClient.close(); } catch (IOException e) { // ignore } } logger.info("Slack RTM closed."); }
websocket.sendBinaryFrame(ECHO_BYTES, false, 0); websocket.sendContinuationFrame(ECHO_BYTES, true, 0); latch.await(); assertEquals(text.get(), "ECHOECHO".getBytes());
public void close() throws IOException { if (websocket != null) { websocket.close(); } if (client != null) { client.close(); } }
void reConnect() throws Exception { if (websocket == null || !websocket.isOpen()) { String uri = getHttpUri().toASCIIString(); LOG.info("Reconnecting websocket: {}", uri); connect(); } }
/** * {@inheritDoc} */ @Override public final void onFailure(Throwable t) { for (WebSocketListener listener : listeners) { if (!ok.get() && webSocket != null) { webSocket.addWebSocketListener(listener); } listener.onError(t); } }
@Test public void streamText() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { getWebSocket(c).sendTextFrame("STREAM", true, 0).get(1, TimeUnit.SECONDS); } }
public void sendTextMessage(String message) { websocket.sendMessage(message); }
@Test(timeOut = 60000) public void streamBytes() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { getWebSocket(c).sendBinaryFrame("STREAM".getBytes(), true, 0).get(1, TimeUnit.SECONDS); } }
@Test(timeOut = 60000) public void onClose() throws Exception { try (AsyncHttpClient c = asyncHttpClient()) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketListener() { @Override public void onOpen(WebSocket websocket) { } @Override public void onClose(WebSocket websocket, int code, String reason) { text.set("OnClose"); latch.countDown(); } @Override public void onError(Throwable t) { t.printStackTrace(); latch.countDown(); } }).build()).get(); websocket.sendCloseFrame(); latch.await(); assertEquals(text.get(), "OnClose"); } }
public void close() throws IOException { if (websocket != null) { websocket.close(); } if (client != null) { client.close(); } }