Search in sources :

Example 11 with TextWebSocketFrame

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

the class AnnotatedEndpointTest method testStringOnMessage.

@Test
public void testStringOnMessage() throws Exception {
    final byte[] payload = "hello".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/chat/Stuart"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "hello Stuart".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Example 12 with TextWebSocketFrame

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

the class AnnotatedEndpointTest method testEncodingWithGenericSuperclass.

@Test
public void testEncodingWithGenericSuperclass() throws Exception {
    final byte[] payload = "hello".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/encodingGenerics/Stuart"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "hello Stuart".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Example 13 with TextWebSocketFrame

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

the class AnnotatedEndpointTest method testRequestUri.

@Test
public void testRequestUri() throws Exception {
    final byte[] payload = "hello".getBytes();
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/request?a=b"));
    client.connect();
    client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "/ws/request?a=b".getBytes(), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) URI(java.net.URI) Test(org.junit.Test)

Example 14 with TextWebSocketFrame

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

the class DeflateEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
    if (encoder == null) {
        encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.NONE, compressionLevel, windowSize, 8));
    }
    encoder.writeOutbound(msg.content().retain());
    CompositeByteBuf fullCompressedContent = ctx.alloc().compositeBuffer();
    for (; ; ) {
        ByteBuf partCompressedContent = encoder.readOutbound();
        if (partCompressedContent == null) {
            break;
        }
        if (!partCompressedContent.isReadable()) {
            partCompressedContent.release();
            continue;
        }
        fullCompressedContent.addComponent(true, partCompressedContent);
    }
    if (fullCompressedContent.numComponents() <= 0) {
        fullCompressedContent.release();
        throw new CodecException("cannot read compressed buffer");
    }
    if (msg.isFinalFragment() && noContext) {
        cleanup();
    }
    ByteBuf compressedContent;
    if (removeFrameTail(msg)) {
        int realLength = fullCompressedContent.readableBytes() - FRAME_TAIL.length;
        compressedContent = fullCompressedContent.slice(0, realLength);
    } else {
        compressedContent = fullCompressedContent;
    }
    WebSocketFrame outMsg;
    if (msg instanceof TextWebSocketFrame) {
        outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof BinaryWebSocketFrame) {
        outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else if (msg instanceof ContinuationWebSocketFrame) {
        outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
    } else {
        throw new CodecException("unexpected frame type: " + msg.getClass().getName());
    }
    out.add(outMsg);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) CodecException(io.netty.handler.codec.CodecException) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) ContinuationWebSocketFrame(io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 15 with TextWebSocketFrame

use of io.netty.handler.codec.http.websocketx.TextWebSocketFrame 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()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        System.out.println("WebSocket Client connected!");
        handshakeFuture.setSuccess();
        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)

Aggregations

TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)26 URI (java.net.URI)16 FrameChecker (io.undertow.websockets.utils.FrameChecker)15 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)15 Test (org.junit.Test)15 FutureResult (org.xnio.FutureResult)15 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)5 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)5 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)5 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)5 UndertowSession (io.undertow.websockets.jsr.UndertowSession)5 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)5 ByteBuf (io.netty.buffer.ByteBuf)4 IOException (java.io.IOException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Endpoint (javax.websocket.Endpoint)4 EndpointConfig (javax.websocket.EndpointConfig)4 MessageHandler (javax.websocket.MessageHandler)4 Session (javax.websocket.Session)4