Search in sources :

Example 16 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.

the class WebSocketImplBase method handleCloseFrame.

private void handleCloseFrame(WebSocketFrameInternal closeFrame) {
    boolean echo;
    synchronized (conn) {
        echo = closeStatusCode == null;
        closed = true;
        closeStatusCode = closeFrame.closeStatusCode();
        closeReason = closeFrame.closeReason();
    }
    handleClose(true);
    if (echo) {
        ChannelPromise fut = conn.channelFuture();
        conn.writeToChannel(new CloseWebSocketFrame(closeStatusCode, closeReason), fut);
        fut.addListener(v -> handleCloseConnection());
    } else {
        handleCloseConnection();
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) ChannelPromise(io.netty.channel.ChannelPromise)

Example 17 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.

the class WebSocketTest method testServerCloseHandshake.

@Test
public void testServerCloseHandshake() {
    short status = (short) (4000 + TestUtils.randomPositiveInt() % 100);
    waitFor(2);
    server = vertx.createHttpServer();
    server.webSocketHandler(ws -> {
        ws.closeHandler(sc -> {
            assertEquals((Short) (short) status, ws.closeStatusCode());
            complete();
        });
    });
    server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> {
        client = vertx.createHttpClient();
        handshake(client, req -> {
            req.send(onSuccess(resp -> {
                assertEquals(101, resp.statusCode());
                Http1xClientConnection conn = (Http1xClientConnection) req.connection();
                NetSocketInternal soi = conn.toNetSocket();
                soi.channelHandlerContext().pipeline().addBefore("handler", "encoder", new WebSocket13FrameEncoder(true));
                soi.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(false, false, 1000));
                String reason = randomAlphaString(10);
                soi.writeMessage(new CloseWebSocketFrame(status, reason));
                AtomicBoolean closeFrameReceived = new AtomicBoolean();
                soi.messageHandler(msg -> {
                    try {
                        if (msg instanceof CloseWebSocketFrame) {
                            CloseWebSocketFrame frame = (CloseWebSocketFrame) msg;
                            assertEquals(status, frame.statusCode());
                            assertEquals(reason, frame.reasonText());
                            closeFrameReceived.set(true);
                        }
                    } finally {
                        ReferenceCountUtil.release(msg);
                    }
                });
                soi.closeHandler(v2 -> {
                    assertTrue(closeFrameReceived.get());
                    complete();
                });
            }));
        });
    }));
    await();
}
Also used : WebSocketInternal(io.vertx.core.http.impl.WebSocketInternal) MultiMap(io.vertx.core.MultiMap) Context(io.vertx.core.Context) Matcher(java.util.regex.Matcher) PlatformDependent(io.netty.util.internal.PlatformDependent) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DEFAULT_HTTP_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_HOST) ReadStream(io.vertx.core.streams.ReadStream) HAProxy(io.vertx.test.proxy.HAProxy) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) CheckingSender(io.vertx.test.core.CheckingSender) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) VertxOptions(io.vertx.core.VertxOptions) Trust(io.vertx.test.tls.Trust) BlockingQueue(java.util.concurrent.BlockingQueue) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Certificate(java.security.cert.Certificate) Buffer(io.vertx.core.buffer.Buffer) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) AbstractVerticle(io.vertx.core.AbstractVerticle) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cert(io.vertx.test.tls.Cert) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) NetSocket(io.vertx.core.net.NetSocket) java.util(java.util) MessageDigest(java.security.MessageDigest) DEFAULT_HTTP_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTP_PORT) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) TestUtils.assertNullPointerException(io.vertx.test.core.TestUtils.assertNullPointerException) VertxTestBase(io.vertx.test.core.VertxTestBase) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) DEFAULT_HTTPS_HOST(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_HOST) BiConsumer(java.util.function.BiConsumer) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocket13FrameDecoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder) AsyncResult(io.vertx.core.AsyncResult) SocketAddress(io.vertx.core.net.SocketAddress) DEFAULT_TEST_URI(io.vertx.core.http.HttpTestBase.DEFAULT_TEST_URI) ConcurrentHashSet(io.vertx.core.impl.ConcurrentHashSet) Promise(io.vertx.core.Promise) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) IOException(java.io.IOException) Http1xServerConnection(io.vertx.core.http.impl.Http1xServerConnection) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WebSocket13FrameEncoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder) DeploymentOptions(io.vertx.core.DeploymentOptions) NetServer(io.vertx.core.net.NetServer) DEFAULT_HTTPS_PORT(io.vertx.core.http.HttpTestBase.DEFAULT_HTTPS_PORT) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) TestUtils.assertIllegalStateException(io.vertx.test.core.TestUtils.assertIllegalStateException) Http1xClientConnection(io.vertx.core.http.impl.Http1xClientConnection) WebSocket13FrameEncoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameEncoder) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Http1xClientConnection(io.vertx.core.http.impl.Http1xClientConnection) WebSocket13FrameDecoder(io.netty.handler.codec.http.websocketx.WebSocket13FrameDecoder) TestUtils.randomAlphaString(io.vertx.test.core.TestUtils.randomAlphaString) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) Test(org.junit.Test)

Example 18 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project async-http-client by AsyncHttpClient.

the class NettyWebSocket method handleFrame.

public void handleFrame(WebSocketFrame frame) {
    if (frame instanceof TextWebSocketFrame) {
        onTextFrame((TextWebSocketFrame) frame);
    } else if (frame instanceof BinaryWebSocketFrame) {
        onBinaryFrame((BinaryWebSocketFrame) frame);
    } else if (frame instanceof CloseWebSocketFrame) {
        Channels.setDiscard(channel);
        CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
        onClose(closeFrame.statusCode(), closeFrame.reasonText());
        Channels.silentlyCloseChannel(channel);
    } else if (frame instanceof PingWebSocketFrame) {
        onPing((PingWebSocketFrame) frame);
    } else if (frame instanceof PongWebSocketFrame) {
        onPong((PongWebSocketFrame) frame);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Example 19 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project reactor-netty by reactor.

the class HttpServerWSOperations method onInboundNext.

@Override
public void onInboundNext(ChannelHandlerContext ctx, Object frame) {
    if (frame instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) frame).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug("CloseWebSocketFrame detected. Closing Websocket");
        }
        CloseWebSocketFrame close = (CloseWebSocketFrame) frame;
        sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content()), f -> onHandlerTerminate());
        return;
    }
    if (frame instanceof PingWebSocketFrame) {
        ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) frame).content()));
        ctx.read();
        return;
    }
    super.onInboundNext(ctx, frame);
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Example 20 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project undertow by undertow-io.

the class WebSocketTestClient method destroy.

/**
 * Destroy the client and also close open connections if any exist
 */
public void destroy() {
    if (!closed) {
        final CountDownLatch latch = new CountDownLatch(1);
        send(new CloseWebSocketFrame(), new FrameListener() {

            @Override
            public void onFrame(WebSocketFrame frame) {
                latch.countDown();
            }

            @Override
            public void onError(Throwable t) {
                latch.countDown();
            }
        });
        try {
            latch.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
    // bootstrap.releaseExternalResources();
    if (ch != null) {
        ch.close().syncUninterruptibly();
    }
    try {
        bootstrap.group().shutdownGracefully(0, 1, TimeUnit.SECONDS).get();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)41 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)23 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)23 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)22 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)17 URI (java.net.URI)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)11 Test (org.junit.jupiter.api.Test)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)10 WebSocketClientHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketClientHandshakeException)10 TimeUnit (java.util.concurrent.TimeUnit)10 Consumer (java.util.function.Consumer)10 Function (java.util.function.Function)10 Unpooled (io.netty.buffer.Unpooled)9 CorruptedFrameException (io.netty.handler.codec.CorruptedFrameException)9