@Test public void testCloseCustomPayloadFromClient() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(STATUS_CODE, REASON); }); }); awaitLatch(latch); }
@Test public void testCloseCustomPayloadFromServer() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); vertx.setTimer(1000, (ar) -> socket.close(STATUS_CODE, REASON)); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }); }); awaitLatch(latch); }
@Test public void testCloseStatusCodeFromClient() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(); }); }); awaitLatch(latch); }
@Test public void testCloseStatusCodeFromServer() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); vertx.setTimer(1000, (ar) -> socket.close()); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }); }); awaitLatch(latch); }
ws.frameHandler(frame -> { currentReceived.get().appendBuffer(frame.binaryData()); if (frame.isFinal()) { received.add(currentReceived.get());
assertTrue(frame.isBinary()); assertFalse(frame.isText()); assertEquals(data, frame.binaryData()); } else { assertFalse(frame.isBinary()); frameConsumer.accept(frame); if (binary) { ws.writeFinalBinaryFrame(frame.binaryData()); } else { ws.writeFinalTextFrame(frame.textData());
Buffer action = frame.binaryData(); LOGGER.info("watching config recieved {}", action); Map<String, Object> mAction = action.toJsonObject().getMap();
@Test public void testCloseCustomPayloadFromClient() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(STATUS_CODE, REASON); }); }); awaitLatch(latch); }
@Test public void testCloseCustomPayloadFromServer() throws InterruptedException { final String REASON = "I'm moving away!"; final short STATUS_CODE = (short)1001; CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); vertx.setTimer(1000, (ar) -> socket.close(STATUS_CODE, REASON)); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(REASON, frame.binaryData().getByteBuf().readerIndex(2).toString(StandardCharsets.UTF_8)); assertEquals(STATUS_CODE, frame.binaryData().getByteBuf().getShort(0)); assertEquals(REASON, frame.closeReason()); assertEquals(STATUS_CODE, frame.closeStatusCode()); latch.countDown(); }); }); }); awaitLatch(latch); }
final Buffer finalBuffer = count[0] > 1 ? createNewBuffer(bufferRef[0], event.binaryData()) : event.binaryData(); if (event.isBinary()) { ((NetSocketBase) webSocket).setBinary(); bufferRef[0] = null; } else { bufferRef[0] = createNewBuffer(bufferRef[0], event.binaryData());
@Test public void testCloseStatusCodeFromClient() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.close(); }); }); awaitLatch(latch); }
@Test public void testCloseStatusCodeFromServer() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); vertx.setTimer(1000, (ar) -> socket.close()); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.frameHandler(frame -> { assertEquals(1000, frame.binaryData().getByteBuf().getShort(0)); assertEquals(1000, frame.closeStatusCode()); assertNull(frame.closeReason()); latch.countDown(); }); }); }); awaitLatch(latch); }
@Test public void testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }
ws.frameHandler(frame -> { currentReceived.get().appendBuffer(frame.binaryData()); if (frame.isFinal()) { received.add(currentReceived.get());
/** * @return the data of the frame */ public io.vertx.rxjava.core.buffer.Buffer binaryData() { if (cached_1 != null) { return cached_1; } io.vertx.rxjava.core.buffer.Buffer ret = io.vertx.rxjava.core.buffer.Buffer.newInstance(delegate.binaryData()); cached_1 = ret; return ret; }
/** * @return the data of the frame */ public io.vertx.rxjava.core.buffer.Buffer binaryData() { if (cached_1 != null) { return cached_1; } io.vertx.rxjava.core.buffer.Buffer ret = io.vertx.rxjava.core.buffer.Buffer.newInstance(delegate.binaryData()); cached_1 = ret; return ret; }
assertTrue(frame.isBinary()); assertFalse(frame.isText()); assertEquals(data, frame.binaryData()); } else { assertFalse(frame.isBinary()); frameConsumer.accept(frame); if (binary) { ws.writeFinalBinaryFrame(frame.binaryData()); } else { ws.writeFinalTextFrame(frame.textData());
private void handleSocketFrame(final WebSocketFrame frame) { try { if(frame.isText()) { handleSocketData(new JsonObject(frame.textData())); } if(frame.isBinary()) { handleBinaryData(frame.binaryData()); } if(frame.isClose()) { handleSocketCloseFrame(frame); } } catch(final Exception e) { catnip.logAdapter().error("Shard {}/{}: Failed to handle socket frame", id, limit, e); } }
final Buffer finalBuffer = count[0] > 1 ? createNewBuffer(bufferRef[0], event.binaryData()) : event.binaryData(); if (event.isBinary()) { ((NetSocketBase) webSocket).setBinary(); bufferRef[0] = null; } else { bufferRef[0] = createNewBuffer(bufferRef[0], event.binaryData());
@Test public void testInvalidMessageCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().addInboundPermitted(new PermittedOptions().setAddress("SockJSHandlerTest.testInvalidMessageCode"))) ); vertx.eventBus().consumer("SockJSHandlerTest.testInvalidMessageCode", msg -> msg.reply(new JsonObject())); client.websocket("/ws-timeout/websocket", ws -> { ws.writeFinalBinaryFrame(Buffer.buffer("durp!")); ws.frameHandler(frame -> { // we should get a normal frame with a error message if (!frame.isClose()) { JsonObject msg = new JsonObject(frame.binaryData()); assertEquals("err", msg.getString("type")); assertEquals("invalid_json", msg.getString("body")); testComplete(); ws.close(); } }); }); await(); } }