Search in sources :

Example 6 with BufferedBinaryMessage

use of io.undertow.websockets.core.BufferedBinaryMessage in project undertow by undertow-io.

the class WebSocket07ServerTest method testPing.

@Test
public void testPing() throws Exception {
    final AtomicBoolean connected = new AtomicBoolean(false);
    DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(new WebSocketConnectionCallback() {

        @Override
        public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
            connected.set(true);
            channel.getReceiveSetter().set(new AbstractReceiveListener() {

                @Override
                protected void onFullPingMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
                    final Pooled<ByteBuffer[]> data = message.getData();
                    WebSockets.sendPong(data.getResource(), channel, new WebSocketCallback<Void>() {

                        @Override
                        public void complete(WebSocketChannel channel, Void context) {
                            data.close();
                        }

                        @Override
                        public void onError(WebSocketChannel channel, Void context, Throwable throwable) {
                            data.close();
                        }
                    });
                }
            });
            channel.resumeReceives();
        }
    }));
    final FutureResult latch = new FutureResult();
    final byte[] payload = "payload".getBytes();
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ':' + DefaultServer.getHostPort("default") + '/'));
    client.connect();
    client.send(new PingWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(PongWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) Pooled(org.xnio.Pooled) IOException(java.io.IOException) PingWebSocketFrame(io.netty.handler.codec.http.websocketx.PingWebSocketFrame) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) PongWebSocketFrame(io.netty.handler.codec.http.websocketx.PongWebSocketFrame) FutureResult(org.xnio.FutureResult) WebSocketCallback(io.undertow.websockets.core.WebSocketCallback) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

Example 7 with BufferedBinaryMessage

use of io.undertow.websockets.core.BufferedBinaryMessage in project undertow by undertow-io.

the class WebSocketExtensionBasicTestCase method testLongMessageWithoutExtensions.

@Test
@Ignore
public void testLongMessageWithoutExtensions() throws Exception {
    XnioWorker client;
    Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
    client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
    WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
    DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
    DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
    final WebSocketClientNegotiation negotiation = null;
    final WebSocketChannel clientChannel = WebSocketClient.connect(client, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI("http://localhost:8080"), WebSocketVersion.V13, negotiation).get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes");
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().close();
            WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            WebSocketLogger.ROOT_LOGGER.info("onError");
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    clientChannel.resumeReceives();
    int LONG_MSG = 75 * 1024;
    StringBuilder longMsg = new StringBuilder(LONG_MSG);
    for (int i = 0; i < LONG_MSG; i++) {
        longMsg.append(new Integer(i).toString().charAt(0));
    }
    StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener(longMsg.toString()).setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals(longMsg.toString(), result.get());
    clientChannel.sendClose();
    client.shutdown();
}
Also used : XnioWorker(org.xnio.XnioWorker) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) Xnio(org.xnio.Xnio) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with BufferedBinaryMessage

use of io.undertow.websockets.core.BufferedBinaryMessage in project undertow by undertow-io.

the class WebSocketExtensionBasicTestCase method testLongTextMessage.

@Test
public void testLongTextMessage() throws Exception {
    XnioWorker client;
    Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
    client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
    WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
    DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
    DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
    final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
    List<WebSocketExtension> extensionsList = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
    final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensionsList);
    Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
    extensionHandshakes.add(new PerMessageDeflateHandshake(true));
    final WebSocketChannel clientChannel = WebSocketClient.connect(client, null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            //WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes.");
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().close();
            WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            WebSocketLogger.ROOT_LOGGER.info("onError");
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    clientChannel.resumeReceives();
    int LONG_MSG = 125 * 1024;
    StringBuilder longMsg = new StringBuilder(LONG_MSG);
    for (int i = 0; i < LONG_MSG; i++) {
        longMsg.append(Integer.toString(i).charAt(0));
    }
    WebSockets.sendTextBlocking(longMsg.toString(), clientChannel);
    latch.await(300, TimeUnit.SECONDS);
    Assert.assertEquals(longMsg.toString(), result.get());
    clientChannel.sendClose();
    client.shutdown();
}
Also used : XnioWorker(org.xnio.XnioWorker) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) Xnio(org.xnio.Xnio) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) HashSet(java.util.HashSet) WebSocketExtension(io.undertow.websockets.WebSocketExtension) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with BufferedBinaryMessage

use of io.undertow.websockets.core.BufferedBinaryMessage in project undertow by undertow-io.

the class JsrWebsocketExtensionTestCase method testExtensionsHeaders.

@Test
public void testExtensionsHeaders() throws Exception {
    final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
    // List format
    final String SEC_WEBSOCKET_EXTENSIONS_EXPECTED = "[permessage-deflate; client_no_context_takeover]";
    List<WebSocketExtension> extensions = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
    final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensions);
    Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
    extensionHandshakes.add(new PerMessageDeflateHandshake(true));
    final WebSocketChannel clientChannel = WebSocketClient.connect(DefaultServer.getWorker(), null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage - Client - Received: " + data.getBytes().length + " bytes . Data: " + data);
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().close();
            WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            WebSocketLogger.ROOT_LOGGER.info("onError");
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    clientChannel.resumeReceives();
    StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener("Hello, World!").setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello, World!", result.get());
    clientChannel.sendClose();
    Assert.assertEquals(SEC_WEBSOCKET_EXTENSIONS_EXPECTED, debug.getResponseExtensions().toString());
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) WebSocketClientNegotiation(io.undertow.websockets.client.WebSocketClientNegotiation) PerMessageDeflateHandshake(io.undertow.websockets.extensions.PerMessageDeflateHandshake) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) HashSet(java.util.HashSet) Test(org.junit.Test) BinaryEndpointTest(io.undertow.websockets.jsr.test.BinaryEndpointTest)

Example 10 with BufferedBinaryMessage

use of io.undertow.websockets.core.BufferedBinaryMessage in project undertow by undertow-io.

the class SuspendResumeTestCase method testConnectionWaitsForMessageEnd.

@Test
public void testConnectionWaitsForMessageEnd() throws Exception {
    final CountDownLatch done = new CountDownLatch(1);
    final AtomicReference<String> message = new AtomicReference<>();
    WebSocketChannel channel = WebSocketClient.connectionBuilder(DefaultServer.getWorker(), DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL() + "/")).connect().get();
    channel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage msg) throws IOException {
            message.set(msg.getData());
            done.countDown();
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            error.printStackTrace();
            message.set("error");
            done.countDown();
        }

        @Override
        protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
            message.getData().free();
            done.countDown();
        }
    });
    channel.resumeReceives();
    Assert.assertTrue(channel.isOpen());
    WebSockets.sendText("Hello World", channel, null);
    Thread.sleep(500);
    serverContainer.pause(null);
    try {
        Assert.assertTrue(done.await(10, TimeUnit.SECONDS));
        Assert.assertEquals("Hello World", message.get());
    } finally {
        serverContainer.resume();
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) Test(org.junit.Test)

Aggregations

BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)10 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)10 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)9 IOException (java.io.IOException)9 URI (java.net.URI)9 Test (org.junit.Test)9 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)6 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)6 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 WebSocketExtension (io.undertow.websockets.WebSocketExtension)4 HashSet (java.util.HashSet)4 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)3 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)3 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)3 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)3 FrameChecker (io.undertow.websockets.utils.FrameChecker)3 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3