Search in sources :

Example 26 with BinaryWebSocketFrame

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

the class JsrWebSocketServer07Test method testBinaryWithByteBuffer.

@org.junit.Test
public void testBinaryWithByteBuffer() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<Throwable> cause = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

                @Override
                public void onMessage(ByteBuffer message) {
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf);
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    Assert.assertNull(cause.get());
    client.destroy();
}
Also used : MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 27 with BinaryWebSocketFrame

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

the class NettyWebSocket method stream.

@Override
public WebSocket stream(final byte[] fragment, final boolean last, final WebSocketWriteCompleteListener listener) {
    final ChannelPromise channelPromise = channel.newPromise();
    channelPromise.addListener(listener);
    channel.writeAndFlush(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment)), channelPromise);
    return this;
}
Also used : BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ChannelPromise(io.netty.channel.ChannelPromise)

Example 28 with BinaryWebSocketFrame

use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame 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)

Aggregations

BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)28 Test (org.junit.Test)20 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)15 ByteBuf (io.netty.buffer.ByteBuf)12 FrameChecker (io.undertow.websockets.utils.FrameChecker)7 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)7 URI (java.net.URI)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 FutureResult (org.xnio.FutureResult)7 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)6 UndertowSession (io.undertow.websockets.jsr.UndertowSession)6 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Endpoint (javax.websocket.Endpoint)6 EndpointConfig (javax.websocket.EndpointConfig)6 MessageHandler (javax.websocket.MessageHandler)6 Session (javax.websocket.Session)6 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)6 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)5 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5