Search in sources :

Example 1 with DirectChannelAccess

use of ratpack.handling.direct.DirectChannelAccess in project ratpack by ratpack.

the class WebSocketEngine method connect.

@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
    PublicAddress publicAddress = context.get(PublicAddress.class);
    URI address = publicAddress.get(context);
    URI httpPath = address.resolve(path);
    URI wsPath;
    try {
        wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
    } catch (URISyntaxException e) {
        throw uncheck(e);
    }
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
    Request request = context.getRequest();
    HttpMethod method = valueOf(request.getMethod().getName());
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
    nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
    nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
    final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
    final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
    final Channel channel = directChannelAccess.getChannel();
    if (!channel.config().isAutoRead()) {
        channel.config().setAutoRead(true);
    }
    handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DirectChannelAccess(ratpack.handling.direct.DirectChannelAccess) Channel(io.netty.channel.Channel) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Request(ratpack.http.Request) PublicAddress(ratpack.server.PublicAddress) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Aggregations

Channel (io.netty.channel.Channel)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 DirectChannelAccess (ratpack.handling.direct.DirectChannelAccess)1 Request (ratpack.http.Request)1 PublicAddress (ratpack.server.PublicAddress)1