Search in sources :

Example 1 with AsyncWebSocketHttpServerExchange

use of io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange 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

HttpServerExchange (io.undertow.server.HttpServerExchange)1 HttpUpgradeListener (io.undertow.server.HttpUpgradeListener)1 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)1 Handshake (io.undertow.websockets.core.protocol.Handshake)1 Hybi07Handshake (io.undertow.websockets.core.protocol.version07.Hybi07Handshake)1 Hybi08Handshake (io.undertow.websockets.core.protocol.version08.Hybi08Handshake)1 Hybi13Handshake (io.undertow.websockets.core.protocol.version13.Hybi13Handshake)1 ExtensionHandshake (io.undertow.websockets.extensions.ExtensionHandshake)1 AsyncWebSocketHttpServerExchange (io.undertow.websockets.spi.AsyncWebSocketHttpServerExchange)1 StreamConnection (org.xnio.StreamConnection)1