Search in sources :

Example 21 with WebSocketChannel

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

the class AbstractWebSocketServerTest method testText.

@Test
public void testText() 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 onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
                    String string = message.getData();
                    if (string.equals("hello")) {
                        WebSockets.sendText("world", channel, null);
                    } else {
                        WebSockets.sendText(string, channel, null);
                    }
                }
            });
            channel.resumeReceives();
        }
    }));
    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 TextWebSocketFrame(Unpooled.copiedBuffer("hello", CharsetUtil.US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(CharsetUtil.US_ASCII), latch));
    latch.getIoFuture().get();
    client.destroy();
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) IOException(java.io.IOException) URI(java.net.URI) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) 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) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) FrameChecker(io.undertow.websockets.utils.FrameChecker) WebSocketProtocolHandshakeHandler(io.undertow.websockets.WebSocketProtocolHandshakeHandler) WebSocketConnectionCallback(io.undertow.websockets.WebSocketConnectionCallback) Test(org.junit.Test)

Example 22 with WebSocketChannel

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

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

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

the class DebugExtensionsListener method onFullTextMessage.

@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
    txtMsgs++;
    String data = message.getData();
    WebSocketLogger.EXTENSION_LOGGER.info("#" + txtMsgs + " onFullTextMessage() - Received: " + data.getBytes().length + " bytes. ");
    for (WebSocketChannel peerChannel : channel.getPeerConnections()) {
        WebSockets.sendText(data, peerChannel, null);
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel)

Example 25 with WebSocketChannel

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

the class DebugExtensionsListener method onFullBinaryMessage.

@Override
protected void onFullBinaryMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
    binMsgs++;
    ByteBuffer[] data = message.getData().getResource();
    int total = 0;
    for (int i = 0; i < data.length; i++) {
        total += data[i].remaining();
    }
    StringBuilder received = new StringBuilder();
    received.append("# " + binMsgs + " onFullBinaryMessage() - Received: ").append(total).append(" length.").append("\n");
    WebSocketLogger.EXTENSION_LOGGER.info(received.toString());
    for (WebSocketChannel peerChannel : channel.getPeerConnections()) {
        WebSockets.sendBinary(data, peerChannel, null);
    }
}
Also used : WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ByteBuffer(java.nio.ByteBuffer)

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