Search in sources :

Example 1 with WebSocketServerHandshaker

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project netty-socketio by mrniko.

the class WebSocketTransport method handshake.

private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) {
    final Channel channel = ctx.channel();
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, configuration.getMaxFramePayloadLength());
    WebSocketServerHandshaker handshaker = factory.newHandshaker(req);
    if (handshaker != null) {
        ChannelFuture f = handshaker.handshake(channel, req);
        f.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    log.error("Can't handshake " + sessionId, future.cause());
                    return;
                }
                channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR, new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength()));
                connectClient(channel, sessionId);
            }
        });
    } else {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) Channel(io.netty.channel.Channel) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 2 with WebSocketServerHandshaker

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project intellij-community by JetBrains.

the class WebSocketHandshakeHandler method handleWebSocketRequest.

private void handleWebSocketRequest(@NotNull final ChannelHandlerContext context, @NotNull FullHttpRequest request, @NotNull final QueryStringDecoder uriDecoder) {
    WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory("ws://" + request.headers().getAsString(HttpHeaderNames.HOST) + uriDecoder.path(), null, false, NettyUtil.MAX_CONTENT_LENGTH);
    WebSocketServerHandshaker handshaker = factory.newHandshaker(request);
    if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(context.channel());
        return;
    }
    if (!context.channel().isOpen()) {
        return;
    }
    final Client client = new WebSocketClient(context.channel(), handshaker);
    context.channel().attr(ClientManagerKt.getCLIENT()).set(client);
    handshaker.handshake(context.channel(), request).addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                ClientManager clientManager = WebSocketHandshakeHandler.this.clientManager.getValue();
                clientManager.addClient(client);
                MessageChannelHandler messageChannelHandler = new MessageChannelHandler(clientManager, getMessageServer());
                BuiltInServer.replaceDefaultHandler(context, messageChannelHandler);
                ChannelHandlerContext messageChannelHandlerContext = context.pipeline().context(messageChannelHandler);
                context.pipeline().addBefore(messageChannelHandlerContext.name(), "webSocketFrameAggregator", new WebSocketFrameAggregator(NettyUtil.MAX_CONTENT_LENGTH));
                messageChannelHandlerContext.channel().attr(ClientManagerKt.getCLIENT()).set(client);
                connected(client, uriDecoder.parameters());
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 3 with WebSocketServerHandshaker

use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project vert.x by eclipse.

the class HttpServerImpl method createHandshaker.

WebSocketServerHandshaker createHandshaker(Channel ch, HttpRequest request) {
    // As a fun part, Firefox 6.0.2 supports Websockets protocol '7'. But,
    // it doesn't send a normal 'Connection: Upgrade' header. Instead it
    // sends: 'Connection: keep-alive, Upgrade'. Brilliant.
    String connectionHeader = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
    if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
        sendError("\"Connection\" must be \"Upgrade\".", BAD_REQUEST, ch);
        return null;
    }
    if (request.getMethod() != HttpMethod.GET) {
        sendError(null, METHOD_NOT_ALLOWED, ch);
        return null;
    }
    try {
        WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ch.pipeline(), request), subProtocols, false, options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames());
        WebSocketServerHandshaker shake = factory.newHandshaker(request);
        if (shake == null) {
            log.error("Unrecognised websockets handshake");
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ch);
        }
        return shake;
    } catch (Exception e) {
        throw new VertxException(e);
    }
}
Also used : WebSocketServerHandshaker(io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker) VertxException(io.vertx.core.VertxException) WebSocketServerHandshakerFactory(io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory) VertxException(io.vertx.core.VertxException) URISyntaxException(java.net.URISyntaxException) Http2Exception(io.netty.handler.codec.http2.Http2Exception) WebSocketHandshakeException(io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)

Aggregations

WebSocketServerHandshaker (io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker)3 WebSocketServerHandshakerFactory (io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 WebSocketFrameAggregator (io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator)2 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 WebSocketHandshakeException (io.netty.handler.codec.http.websocketx.WebSocketHandshakeException)1 Http2Exception (io.netty.handler.codec.http2.Http2Exception)1 VertxException (io.vertx.core.VertxException)1 URISyntaxException (java.net.URISyntaxException)1