Search in sources :

Example 31 with CloseWebSocketFrame

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

the class WebSocketTransportTest method testCloseFrame.

/**
 * Test method for {@link com.corundumstudio.socketio.transport.WebSocketTransport#channelRead()}.
 */
@Test
public void testCloseFrame() {
    EmbeddedChannel channel = createChannel();
    channel.writeInbound(new CloseWebSocketFrame());
    Object msg = channel.readOutbound();
    // https://tools.ietf.org/html/rfc6455#section-5.5.1
    // If an endpoint receives a Close frame and did not previously send a Close frame, the endpoint
    // MUST send a Close frame in response.
    assertTrue(msg instanceof CloseWebSocketFrame);
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.Test)

Example 32 with CloseWebSocketFrame

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

the class WebSocketExtensionFilterTest method testAlwaysSkip.

@Test
public void testAlwaysSkip() {
    WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.ALWAYS_SKIP;
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
    assertTrue(neverSkip.mustSkip(binaryFrame));
    assertTrue(binaryFrame.release());
    TextWebSocketFrame textFrame = new TextWebSocketFrame();
    assertTrue(neverSkip.mustSkip(textFrame));
    assertTrue(textFrame.release());
    PingWebSocketFrame pingFrame = new PingWebSocketFrame();
    assertTrue(neverSkip.mustSkip(pingFrame));
    assertTrue(pingFrame.release());
    PongWebSocketFrame pongFrame = new PongWebSocketFrame();
    assertTrue(neverSkip.mustSkip(pongFrame));
    assertTrue(pongFrame.release());
    CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
    assertTrue(neverSkip.mustSkip(closeFrame));
    assertTrue(closeFrame.release());
    ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
    assertTrue(neverSkip.mustSkip(continuationFrame));
    assertTrue(continuationFrame.release());
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Test(org.junit.jupiter.api.Test)

Example 33 with CloseWebSocketFrame

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

the class WebSocketExtensionFilterTest method testNeverSkip.

@Test
public void testNeverSkip() {
    WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.NEVER_SKIP;
    BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
    assertFalse(neverSkip.mustSkip(binaryFrame));
    assertTrue(binaryFrame.release());
    TextWebSocketFrame textFrame = new TextWebSocketFrame();
    assertFalse(neverSkip.mustSkip(textFrame));
    assertTrue(textFrame.release());
    PingWebSocketFrame pingFrame = new PingWebSocketFrame();
    assertFalse(neverSkip.mustSkip(pingFrame));
    assertTrue(pingFrame.release());
    PongWebSocketFrame pongFrame = new PongWebSocketFrame();
    assertFalse(neverSkip.mustSkip(pongFrame));
    assertTrue(pongFrame.release());
    CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
    assertFalse(neverSkip.mustSkip(closeFrame));
    assertTrue(closeFrame.release());
    ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
    assertFalse(neverSkip.mustSkip(continuationFrame));
    assertTrue(continuationFrame.release());
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) Test(org.junit.jupiter.api.Test)

Example 34 with CloseWebSocketFrame

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

the class WebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
        try {
            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
            System.out.println("WebSocket Client connected!");
            handshakeFuture.setSuccess();
        } catch (WebSocketHandshakeException e) {
            System.out.println("WebSocket Client failed to connect");
            handshakeFuture.setFailure(e);
        }
        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;
        System.out.println("WebSocket Client received message: " + textFrame.text());
    } else if (frame instanceof PongWebSocketFrame) {
        System.out.println("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        System.out.println("WebSocket Client received closing");
        ch.close();
    }
}
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) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)

Example 35 with CloseWebSocketFrame

use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project autobahn-java by crossbario.

the class NettyWebSocketClientHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (!mHandshaker.isHandshakeComplete()) {
        FullHttpResponse response = (FullHttpResponse) msg;
        String negotiatedSerializer = response.headers().get("Sec-WebSocket-Protocol");
        LOGGER.d(String.format("Negotiated serializer=%s", negotiatedSerializer));
        ISerializer serializer = initializeSerializer(negotiatedSerializer);
        mHandshaker.finishHandshake(ch, response);
        mHandshakeFuture.setSuccess();
        mTransportHandler.onConnect(mTransport, serializer);
    } else if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    } else if (msg instanceof BinaryWebSocketFrame) {
        BinaryWebSocketFrame binaryWebSocketFrame = (BinaryWebSocketFrame) msg;
        byte[] payload = new byte[binaryWebSocketFrame.content().readableBytes()];
        LOGGER.v(String.format("Received binary frame, content length=%s", payload.length));
        binaryWebSocketFrame.content().readBytes(payload);
        mTransportHandler.onMessage(payload, true);
    } else if (msg instanceof TextWebSocketFrame) {
        TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
        byte[] payload = new byte[textWebSocketFrame.content().readableBytes()];
        LOGGER.v(String.format("Received Text frame, content length=%s", payload.length));
        textWebSocketFrame.content().readBytes(payload);
        mTransportHandler.onMessage(payload, false);
    } else if (msg instanceof PingWebSocketFrame) {
        PingWebSocketFrame pingWebSocketFrame = (PingWebSocketFrame) msg;
        ctx.writeAndFlush(new PongWebSocketFrame(pingWebSocketFrame.content().retain()));
    } else if (msg instanceof PongWebSocketFrame) {
        // Not really doing anything here.
        LOGGER.v("WebSocket Client received pong.");
    } else if (msg instanceof CloseWebSocketFrame) {
        CloseWebSocketFrame closeWebSocketFrame = (CloseWebSocketFrame) msg;
        LOGGER.d(String.format("Received Close frame, code=%s, reason=%s", closeWebSocketFrame.statusCode(), closeWebSocketFrame.reasonText()));
        close(ctx, closeWebSocketFrame.statusCode() == 1000, new CloseDetails(CloseDetails.REASON_DEFAULT, null));
    }
}
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) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) CloseDetails(io.crossbar.autobahn.wamp.types.CloseDetails) ISerializer(io.crossbar.autobahn.wamp.interfaces.ISerializer)

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