@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 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 testCloseFrame() throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { if (frame.isText()) { assertIllegalStateException(frame::closeStatusCode); } else { assertEquals(frame.closeReason(), "It was a good talk"); assertEquals(frame.closeStatusCode(), 1001); } latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.writeTextMessage("Hello"); ws.close((short)1001, "It was a good talk"); }); }); 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 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); }
/** * @return status code of close frame. Only use this for close frames */ public short closeStatusCode() { short ret = delegate.closeStatusCode(); return ret; }
/** * @return status code of close frame. Only use this for close frames */ public short closeStatusCode() { short ret = delegate.closeStatusCode(); return ret; }
@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 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 testCloseFrame() throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); client = vertx.createHttpClient(); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)) .websocketHandler(socket -> { socket.closeHandler(a -> { latch.countDown(); }); socket.frameHandler(frame -> { if (frame.isText()) { assertIllegalStateException(frame::closeStatusCode); } else { assertEquals(frame.closeReason(), "It was a good talk"); assertEquals(frame.closeStatusCode(), 1001); } latch.countDown(); }); }) .listen(ar -> { client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> { ws.writeTextMessage("Hello"); ws.close((short)1001, "It was a good talk"); }); }); 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 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 testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }
private void handleSocketCloseFrame(final WebSocketFrame frame) { socketOpen = false; final short closeCode = frame.closeStatusCode(); if(closeCode == GatewayCloseCode.INVALID_SEQ.code() || closeCode == GatewayCloseCode.SESSION_TIMEOUT.code()) { // These two close codes invalidate your session (and afaik do not send an OP9). catnip.sessionManager().clearSeqnum(id); catnip.sessionManager().clearSession(id); } if(closedByClient) { catnip.logAdapter().info("Shard {}/{}: We closed the websocket with code {}", id, limit, closeCode); } else { if(closeCode >= 4000) { final GatewayCloseCode code = GatewayCloseCode.byId(closeCode); if(code != null) { catnip.logAdapter().warn("Shard {}/{}: gateway websocket closed with code {}: {}: {}", id, limit, closeCode, code.name(), code.message()); } else { catnip.logAdapter().warn("Shard {}/{}: gateway websocket closing with code {}: {}", id, limit, closeCode, frame.closeReason()); } } else { catnip.logAdapter().warn("Shard {}/{}: gateway websocket closing with code {}: {}", id, limit, closeCode, frame.closeReason()); } } }
@Test public void testTimeoutCloseCode() { router.route("/ws-timeout/*").handler(SockJSHandler .create(vertx) .bridge(new BridgeOptions().setPingTimeout(1)) ); client.websocket("/ws-timeout/websocket", ws -> ws.frameHandler(frame -> { if (frame.isClose()) { assertEquals(1001, frame.closeStatusCode()); assertEquals("Session expired", frame.closeReason()); testComplete(); } })); await(); }