Search in sources :

Example 26 with StreamConnection

use of org.xnio.StreamConnection in project undertow by undertow-io.

the class WebSocketProtocolHandshakeHandler method handleRequest.

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (!exchange.getRequestMethod().equals(Methods.GET)) {
        // Only GET is supported to start the handshake
        next.handleRequest(exchange);
        return;
    }
    final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
    Handshake handshaker = null;
    for (Handshake method : handshakes) {
        if (method.matches(facade)) {
            handshaker = method;
            break;
        }
    }
    if (handshaker == null) {
        next.handleRequest(exchange);
    } else {
        WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
        final Handshake selected = handshaker;
        if (upgradeListener == null) {
            exchange.upgradeChannel(new HttpUpgradeListener() {

                @Override
                public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
                    WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
                    peerConnections.add(channel);
                    callback.onConnect(facade, channel);
                }
            });
        } else {
            exchange.upgradeChannel(upgradeListener);
        }
        handshaker.handshake(facade);
    }
}
Also used : AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpServerExchange(io.undertow.server.HttpServerExchange) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) AsyncWebSocketHttpServerExchange(io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange) HttpUpgradeListener(io.undertow.server.HttpUpgradeListener) StreamConnection(org.xnio.StreamConnection) Hybi07Handshake(io.undertow.websockets.core.protocol.version07.Hybi07Handshake) Hybi13Handshake(io.undertow.websockets.core.protocol.version13.Hybi13Handshake) ExtensionHandshake(io.undertow.websockets.extensions.ExtensionHandshake) Handshake(io.undertow.websockets.core.protocol.Handshake) Hybi08Handshake(io.undertow.websockets.core.protocol.version08.Hybi08Handshake)

Aggregations

StreamConnection (org.xnio.StreamConnection)26 IOException (java.io.IOException)11 HttpServerExchange (io.undertow.server.HttpServerExchange)10 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)8 InetSocketAddress (java.net.InetSocketAddress)7 ChannelListener (org.xnio.ChannelListener)7 ConduitStreamSinkChannel (org.xnio.conduits.ConduitStreamSinkChannel)7 OptionMap (org.xnio.OptionMap)6 HttpHandler (io.undertow.server.HttpHandler)4 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)4 Handshake (io.undertow.websockets.core.protocol.Handshake)4 ConduitStreamSourceChannel (org.xnio.conduits.ConduitStreamSourceChannel)4 ByteBufferPool (io.undertow.connector.ByteBufferPool)3 DefaultByteBufferPool (io.undertow.server.DefaultByteBufferPool)3 HttpOpenListener (io.undertow.server.protocol.http.HttpOpenListener)3 HttpServerConnection (io.undertow.server.protocol.http.HttpServerConnection)3 ByteBuffer (java.nio.ByteBuffer)3 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)2 Http2Channel (io.undertow.protocols.http2.Http2Channel)2 SessionConfig (io.undertow.server.session.SessionConfig)2