Search in sources :

Example 11 with WebSocketExtension

use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.

the class ServerWebSocketContainer method toExtensionList.

private static List<WebSocketExtension> toExtensionList(final List<Extension> extensions) {
    List<WebSocketExtension> ret = new ArrayList<>();
    for (Extension e : extensions) {
        final List<WebSocketExtension.Parameter> parameters = new ArrayList<>();
        for (Extension.Parameter p : e.getParameters()) {
            parameters.add(new WebSocketExtension.Parameter(p.getName(), p.getValue()));
        }
        ret.add(new WebSocketExtension(e.getName(), parameters));
    }
    return ret;
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) Extension(javax.websocket.Extension) WebSocketExtension(io.undertow.websockets.WebSocketExtension) ArrayList(java.util.ArrayList)

Example 12 with WebSocketExtension

use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.

the class WebSocket13ClientHandshake method createChannel.

@Override
public WebSocketChannel createChannel(final StreamConnection channel, final String wsUri, final ByteBufferPool bufferPool, OptionMap options) {
    if (negotiation != null && negotiation.getSelectedExtensions() != null && !negotiation.getSelectedExtensions().isEmpty()) {
        List<WebSocketExtension> selected = negotiation.getSelectedExtensions();
        List<ExtensionFunction> negotiated = new ArrayList<>();
        if (selected != null && !selected.isEmpty()) {
            for (WebSocketExtension ext : selected) {
                for (ExtensionHandshake extHandshake : extensions) {
                    if (ext.getName().equals(extHandshake.getName())) {
                        negotiated.add(extHandshake.create());
                    }
                }
            }
        }
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation.getSelectedSubProtocol(), true, !negotiated.isEmpty(), CompositeExtensionFunction.compose(negotiated), new HashSet<WebSocketChannel>(), options);
    } else {
        return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation != null ? negotiation.getSelectedSubProtocol() : "", true, false, NoopExtensionFunction.INSTANCE, new HashSet<WebSocketChannel>(), options);
    }
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension) NoopExtensionFunction(io.undertow.websockets.extensions.NoopExtensionFunction) ExtensionFunction(io.undertow.websockets.extensions.ExtensionFunction) CompositeExtensionFunction(io.undertow.websockets.extensions.CompositeExtensionFunction) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) ArrayList(java.util.ArrayList) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) WebSocket13Channel(io.undertow.websockets.core.protocol.version13.WebSocket13Channel)

Example 13 with WebSocketExtension

use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.

the class PerMessageDeflateHandshake method accept.

@Override
public WebSocketExtension accept(final WebSocketExtension extension) {
    if (extension == null || !extension.getName().equals(getName()))
        return null;
    WebSocketExtension negotiated = new WebSocketExtension(extension.getName());
    if (extension.getParameters() == null || extension.getParameters().size() == 0)
        return negotiated;
    for (WebSocketExtension.Parameter parameter : extension.getParameters()) {
        if (parameter.getName().equals(SERVER_MAX_WINDOW_BITS)) {
        /*
                    Not supported
                 */
        } else if (parameter.getName().equals(CLIENT_MAX_WINDOW_BITS)) {
        /*
                    Not supported
                 */
        } else if (parameter.getName().equals(SERVER_NO_CONTEXT_TAKEOVER)) {
            negotiated.getParameters().add(parameter);
            if (client) {
                decompressContextTakeover = false;
            } else {
                compressContextTakeover = false;
            }
        } else if (parameter.getName().equals(CLIENT_NO_CONTEXT_TAKEOVER)) {
            negotiated.getParameters().add(parameter);
            if (client) {
                compressContextTakeover = false;
            } else {
                decompressContextTakeover = false;
            }
        } else {
            WebSocketLogger.EXTENSION_LOGGER.incorrectExtensionParameter(parameter);
            return null;
        }
    }
    WebSocketLogger.EXTENSION_LOGGER.debugf("Negotiated extension %s for handshake %s", negotiated, extension);
    return negotiated;
}
Also used : WebSocketExtension(io.undertow.websockets.WebSocketExtension)

Example 14 with WebSocketExtension

use of io.undertow.websockets.WebSocketExtension 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)

Aggregations

WebSocketExtension (io.undertow.websockets.WebSocketExtension)14 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)7 ArrayList (java.util.ArrayList)7 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)6 WebSocketClientNegotiation (io.undertow.websockets.client.WebSocketClientNegotiation)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 AbstractReceiveListener (io.undertow.websockets.core.AbstractReceiveListener)4 BufferedBinaryMessage (io.undertow.websockets.core.BufferedBinaryMessage)4 BufferedTextMessage (io.undertow.websockets.core.BufferedTextMessage)4 URI (java.net.URI)4 HashSet (java.util.HashSet)4 Extension (javax.websocket.Extension)4 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 StringWriteChannelListener (io.undertow.util.StringWriteChannelListener)2 WebSocketProtocolHandshakeHandler (io.undertow.websockets.WebSocketProtocolHandshakeHandler)2 StreamSinkFrameChannel (io.undertow.websockets.core.StreamSinkFrameChannel)2 ExtensionFunction (io.undertow.websockets.extensions.ExtensionFunction)2