Search in sources :

Example 6 with WebSocketChannel

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

the class WebSocketClient13TestCase method testTextMessageWss.

@Test
public void testTextMessageWss() throws Exception {
    UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
    final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI("wss://" + 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) Test(org.junit.Test)

Example 7 with WebSocketChannel

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

the class WebSocketClient13TestCase method testMessageViaWssProxy.

@Test
@ProxyIgnore
public void testMessageViaWssProxy() throws Exception {
    final WebSocketChannel webSocketChannel = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(DefaultServer.getDefaultServerSSLAddress())).setSsl(new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext())).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.getHostSSLPort("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) UndertowXnioSsl(io.undertow.protocols.ssl.UndertowXnioSsl) Test(org.junit.Test) ProxyIgnore(io.undertow.testutils.ProxyIgnore)

Example 8 with WebSocketChannel

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

the class AbstractWebSocketServerTest method testBinary.

@Test
public void testBinary() 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 onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
                    final Pooled<ByteBuffer[]> data = message.getData();
                    WebSockets.sendBinary(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 BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) Pooled(org.xnio.Pooled) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) IOException(java.io.IOException) 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) 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 9 with WebSocketChannel

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

the class UndertowSession method setupWebSocketChannel.

private void setupWebSocketChannel(WebSocketChannel webSocketChannel) {
    this.frameHandler = new FrameHandler(this, this.endpoint.getInstance());
    webSocketChannel.getReceiveSetter().set(frameHandler);
    webSocketChannel.addCloseTask(new ChannelListener<WebSocketChannel>() {

        @Override
        public void handleEvent(WebSocketChannel channel) {
            //so this puts us in an interesting position. We know the underlying
            //TCP connection has been torn down, however this may have involved reading
            //a close frame, which will be delivered shortly
            //to get around this we schedule the code in the IO thread, so if there is a close
            //frame awaiting delivery it will be delivered before the close
            channel.getIoThread().execute(new Runnable() {

                @Override
                public void run() {
                    //we delegate this execution to the IO thread
                    try {
                        closeInternal(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, null));
                    } catch (IOException e) {
                    //ignore
                    }
                }
            });
        }
    });
}
Also used : CloseReason(javax.websocket.CloseReason) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) IOException(java.io.IOException)

Example 10 with WebSocketChannel

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

the class FrameHandler method onBinary.

@Override
protected void onBinary(WebSocketChannel webSocketChannel, StreamSourceFrameChannel messageChannel) throws IOException {
    if (session.isSessionClosed()) {
        //to bad, the channel has already been closed
        //we just ignore messages that are received after we have closed, as the endpoint is no longer in a valid state to deal with them
        //this this should only happen if a message was on the wire when we called close()
        messageChannel.close();
        return;
    }
    final HandlerWrapper handler = getHandler(FrameType.BYTE);
    if (handler != null && handler.isPartialHandler()) {
        BufferedBinaryMessage data = new BufferedBinaryMessage(session.getMaxBinaryMessageBufferSize(), false);
        data.read(messageChannel, new WebSocketCallback<BufferedBinaryMessage>() {

            @Override
            public void complete(WebSocketChannel channel, BufferedBinaryMessage context) {
                invokeBinaryHandler(context, handler, context.isComplete());
            }

            @Override
            public void onError(WebSocketChannel channel, BufferedBinaryMessage context, Throwable throwable) {
                invokeOnError(throwable);
            }
        });
    } else {
        bufferFullMessage(messageChannel);
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) BufferedBinaryMessage(io.undertow.websockets.core.BufferedBinaryMessage)

Aggregations

WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)38 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)18 IOException (java.io.IOException)18 URI (java.net.URI)17 Test (org.junit.Test)17 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)16 BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)7 WebSocketConnectionCallback (io.undertow.websockets.WebSocketConnectionCallback)7 WebSocketExtension (io.undertow.websockets.WebSocketExtension)7 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)7 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)7 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)7 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)6 HttpServerExchange (io.undertow.server.HttpServerExchange)5 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)5 ByteBuffer (java.nio.ByteBuffer)5 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)4