All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.reactivex.netty.protocol.http.websocket.WebSocketClient Maven / Gradle / Ivy

There is a newer version: 0.3.18
Show newest version
/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.reactivex.netty.protocol.http.websocket;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPipelineException;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.reactivex.netty.channel.ObservableConnection;
import io.reactivex.netty.client.ClientChannelFactory;
import io.reactivex.netty.client.ClientConnectionFactory;
import io.reactivex.netty.client.ClientMetricsEvent;
import io.reactivex.netty.client.RxClientImpl;
import io.reactivex.netty.metrics.MetricEventsSubject;
import io.reactivex.netty.pipeline.PipelineConfigurator;
import rx.Observable;
import rx.Observable.Operator;
import rx.Subscriber;

/**
 * {@link WebSocketClient} delays connection handling to application subscriber
 * until the WebSocket handshake is complete.
 *
 * @author Tomasz Bak
 */
public class WebSocketClient extends RxClientImpl {

    @SuppressWarnings("rawtypes")
    private static final HandshakeOperator HANDSHAKE_OPERATOR = new HandshakeOperator();

    public WebSocketClient(String name, ServerInfo serverInfo, Bootstrap clientBootstrap,
                           PipelineConfigurator pipelineConfigurator,
                           ClientConfig clientConfig, ClientChannelFactory channelFactory,
                           ClientConnectionFactory> connectionFactory,
                           MetricEventsSubject> eventsSubject) {
        super(name, serverInfo, clientBootstrap, pipelineConfigurator, clientConfig, channelFactory, connectionFactory, eventsSubject);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Observable> connect() {
        return super.connect().lift(HANDSHAKE_OPERATOR);
    }

    static class HandshakeOperator implements Operator, ObservableConnection> {
        @Override
        public Subscriber> call(final Subscriber> originalSubscriber) {
            Subscriber> liftSubscriber = new Subscriber>() {
                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    originalSubscriber.onError(e);
                }

                @Override
                public void onNext(final ObservableConnection connection) {
                    final ChannelPipeline p = connection.getChannel().pipeline();
                    ChannelHandlerContext hctx = p.context(WebSocketClientHandler.class);
                    if (hctx != null) {
                        WebSocketClientHandler handler = p.get(WebSocketClientHandler.class);
                        handler.addHandshakeFinishedListener(new ChannelFutureListener() {
                            @Override
                            public void operationComplete(ChannelFuture future) throws Exception {
                                originalSubscriber.onNext(connection);
                                originalSubscriber.onCompleted();
                            }
                        });
                    } else {
                        originalSubscriber.onError(new ChannelPipelineException(
                                "invalid pipeline configuration - WebSocket pipeline with no WebSocketClientHandler"));
                    }
                }
            };
            originalSubscriber.add(liftSubscriber);
            return liftSubscriber;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy