Search in sources :

Example 11 with AbstractReceiveListener

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

the class AbstractWebSocketServerTest method testCloseFrame.

@Test
public void testCloseFrame() throws Exception {
    if (getVersion() == WebSocketVersion.V00) {
        // ignore 00 tests for now
        return;
    }
    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 onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
                    message.getData().close();
                    channel.sendClose();
                }
            });
            channel.resumeReceives();
        }
    }));
    final AtomicBoolean receivedResponse = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new CloseWebSocketFrame(), new FrameChecker(CloseWebSocketFrame.class, new byte[0], latch));
    latch.getIoFuture().get();
    Assert.assertFalse(receivedResponse.get());
    client.destroy();
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) IOException(java.io.IOException) URI(java.net.URI) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) 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 12 with AbstractReceiveListener

use of io.undertow.websockets.core.AbstractReceiveListener 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 13 with AbstractReceiveListener

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

the class WebSocketClient13TestCase method testTextMessageSecure.

public void testTextMessageSecure(final String urlProtocol) throws Exception {
    UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
    final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(urlProtocol + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"))).setSsl(ssl);
    IoFuture<WebSocketChannel> future = connectionBuilder.connect();
    future.await(4, TimeUnit.SECONDS);
    final WebSocketChannel webSocketChannel = future.get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    webSocketChannel.resumeReceives();
    StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener("Hello World").setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello World", result.get());
    webSocketChannel.sendClose();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) WebSocketClient(io.undertow.websockets.client.WebSocketClient) CountDownLatch(java.util.concurrent.CountDownLatch) StreamSinkFrameChannel(io.undertow.websockets.core.StreamSinkFrameChannel) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl)

Example 14 with AbstractReceiveListener

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

the class WebSocketClient13TestCase method testMessageViaProxy.

@Test
@ProxyIgnore
public void testMessageViaProxy() throws Exception {
    final WebSocketChannel webSocketChannel = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerURL())).setProxyUri(new URI("http", null, DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default") + 10, "/proxy", null, null)).connect().get();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<String> result = new AtomicReference<>();
    webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {

        @Override
        protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
            String data = message.getData();
            result.set(data);
            latch.countDown();
        }

        @Override
        protected void onError(WebSocketChannel channel, Throwable error) {
            super.onError(channel, error);
            error.printStackTrace();
            latch.countDown();
        }
    });
    webSocketChannel.resumeReceives();
    StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
    new StringWriteChannelListener("Hello World").setup(sendChannel);
    latch.await(10, TimeUnit.SECONDS);
    Assert.assertEquals("Hello World", result.get());
    webSocketChannel.sendClose();
    Assert.assertEquals("CONNECT " + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default"), connectLog.poll());
}
Also used : 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) AbstractReceiveListener(io.undertow.websockets.core.AbstractReceiveListener) StringWriteChannelListener(io.undertow.util.StringWriteChannelListener) Test(org.junit.Test) ProxyIgnore(io.undertow.testutils.ProxyIgnore)

Example 15 with AbstractReceiveListener

use of io.undertow.websockets.core.AbstractReceiveListener 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) {
            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) {
            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) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) Test(org.junit.Test)

Aggregations

AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)21 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)21 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)18 URI (java.net.URI)16 Test (org.junit.Test)15 IOException (java.io.IOException)14 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)9 BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)9 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)9 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)8 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)8 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)8 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)5 FrameChecker (io.undertow.websockets.utils.FrameChecker)5 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 FutureResult (org.xnio.FutureResult)5 WebSocketExtension (io.undertow.websockets.WebSocketExtension)4