Search in sources :

Example 1 with CloseWebSocketFrame

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

the class ServerConnection method close.

@Override
public void close() {
    if (handshaker == null) {
        super.close();
    } else {
        endReadAndFlush();
        handshaker.close(channel, new CloseWebSocketFrame(1000, null));
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)

Example 2 with CloseWebSocketFrame

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

the class DefaultWebSocket method close.

@Override
public void close(int statusCode, String reason) {
    open.set(false);
    channel.writeAndFlush(new CloseWebSocketFrame(statusCode, reason));
    channel.close().addListener(future -> onClose.run());
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)

Example 3 with CloseWebSocketFrame

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

the class HttpClientWSOperations method sendClose.

void sendClose(CloseWebSocketFrame frame) {
    if (frame != null && !frame.isFinalFragment()) {
        channel().writeAndFlush(frame);
        return;
    }
    if (CLOSE_SENT.getAndSet(this, 1) == 0) {
        ChannelFuture f = channel().writeAndFlush(frame == null ? new CloseWebSocketFrame() : frame);
        f.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)

Example 4 with CloseWebSocketFrame

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

the class HttpClientWSOperations method onInboundNext.

@Override
@SuppressWarnings("unchecked")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof FullHttpResponse) {
        started = true;
        channel().pipeline().remove(HttpObjectAggregator.class);
        FullHttpResponse response = (FullHttpResponse) msg;
        setNettyResponse(response);
        if (checkResponseCode(response)) {
            try {
                if (!handshaker.isHandshakeComplete()) {
                    handshaker.finishHandshake(channel(), response);
                }
            } catch (WebSocketHandshakeException wshe) {
                if (serverError) {
                    onInboundError(wshe);
                    return;
                }
            } finally {
                // Release unused content (101 status)
                response.content().release();
            }
            parentContext().fireContextActive(this);
            handshakerResult.trySuccess();
        }
        return;
    }
    if (msg instanceof PingWebSocketFrame) {
        channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content()));
        ctx.read();
        return;
    }
    if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
        if (log.isDebugEnabled()) {
            log.debug("CloseWebSocketFrame detected. Closing Websocket");
        }
        CloseWebSocketFrame close = (CloseWebSocketFrame) msg;
        sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content()));
    } else {
        super.onInboundNext(ctx, msg);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Example 5 with CloseWebSocketFrame

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

the class WebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        logger.info("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        isOpen = true;
        return;
    }
    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }
    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        textReceived = textFrame.text();
    } else if (frame instanceof BinaryWebSocketFrame) {
        BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame;
        bufferReceived = binaryFrame.content().nioBuffer();
    } else if (frame instanceof PingWebSocketFrame) {
        PingWebSocketFrame pingFrame = (PingWebSocketFrame) frame;
        isPing = true;
        bufferReceived = pingFrame.content().nioBuffer();
    } else if (frame instanceof PongWebSocketFrame) {
        PongWebSocketFrame pongFrame = (PongWebSocketFrame) frame;
        isPong = true;
        bufferReceived = pongFrame.content().nioBuffer();
    } else if (frame instanceof CloseWebSocketFrame) {
        ch.close().sync();
        isOpen = false;
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) Channel(io.netty.channel.Channel) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame)

Aggregations

CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)30 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)14 PingWebSocketFrame (io.netty.handler.codec.http.websocketx.PingWebSocketFrame)12 PongWebSocketFrame (io.netty.handler.codec.http.websocketx.PongWebSocketFrame)11 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)8 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)7 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 Channel (io.netty.channel.Channel)5 Test (org.junit.Test)4 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)3 FrameChecker (io.undertow.websockets.utils.FrameChecker)3 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)3 URI (java.net.URI)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 FutureResult (org.xnio.FutureResult)3 ByteBuf (io.netty.buffer.ByteBuf)2 ChannelFuture (io.netty.channel.ChannelFuture)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)2