Search in sources :

Example 1 with WebSocketClientCompressionHandler

use of io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler in project alien4cloud by alien4cloud.

the class StompConnection method init.

@SneakyThrows({ InterruptedException.class, URISyntaxException.class })
private void init() {
    if (this.stompChannel != null) {
        throw new IllegalStateException("The stomp connection has already been started");
    }
    String wsUrl = "ws://" + host + ":" + port + endPoint + "/websocket";
    if (log.isDebugEnabled()) {
        log.debug("Web socket url {}", wsUrl);
    }
    String loginUrl = null;
    if (user != null && password != null && loginPath != null) {
        loginUrl = "http://" + host + ":" + port + loginPath;
        if (log.isDebugEnabled()) {
            log.debug("Authentication url {}", loginUrl);
        }
    }
    this.eventLoopGroup = new NioEventLoopGroup();
    this.stompClientHandler = new StompClientHandler();
    DefaultHttpHeaders handshakeHeaders = new DefaultHttpHeaders();
    if (this.headers != null) {
        for (Map.Entry<String, String> header : this.headers.entrySet()) {
            handshakeHeaders.add(header.getKey(), header.getValue());
        }
    }
    final WebSocketClientHandler webSocketHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(new URI(wsUrl), WebSocketVersion.V13, null, false, handshakeHeaders), host, user, password, loginUrl);
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(HttpClientCodec.class.getName(), new HttpClientCodec());
            pipeline.addLast(HttpObjectAggregator.class.getName(), new HttpObjectAggregator(8192));
            pipeline.addLast(WebSocketClientCompressionHandler.class.getName(), new WebSocketClientCompressionHandler());
            pipeline.addLast(WebSocketClientHandler.class.getName(), webSocketHandler);
            pipeline.addLast(StompSubframeDecoder.class.getName(), new StompSubframeDecoder());
            pipeline.addLast(StompSubframeEncoder.class.getName(), new StompSubframeEncoder());
            pipeline.addLast(StompSubframeAggregator.class.getName(), new StompSubframeAggregator(1048576));
            pipeline.addLast(StompClientHandler.class.getName(), stompClientHandler);
        }
    });
    this.stompChannel = b.connect(host, port).sync().channel();
    this.stompClientHandler.connectFuture(this.stompChannel.newPromise());
    webSocketHandler.handshakeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            stompClientHandler.beginStomp(stompChannel);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) WebSocketClientCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler) StompSubframeAggregator(io.netty.handler.codec.stomp.StompSubframeAggregator) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) StompSubframeEncoder(io.netty.handler.codec.stomp.StompSubframeEncoder) URI(java.net.URI) ChannelFutureListener(io.netty.channel.ChannelFutureListener) URISyntaxException(java.net.URISyntaxException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) StompSubframeDecoder(io.netty.handler.codec.stomp.StompSubframeDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SneakyThrows(lombok.SneakyThrows)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)1 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)1 WebSocketClientCompressionHandler (io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler)1 StompSubframeAggregator (io.netty.handler.codec.stomp.StompSubframeAggregator)1 StompSubframeDecoder (io.netty.handler.codec.stomp.StompSubframeDecoder)1 StompSubframeEncoder (io.netty.handler.codec.stomp.StompSubframeEncoder)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 SneakyThrows (lombok.SneakyThrows)1