List of usage examples for io.netty.handler.codec.http.websocketx WebSocketClientProtocolHandler WebSocketClientProtocolHandler
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker)
From source file:com.barchart.netty.client.transport.WebSocketTransport.java
License:BSD License
@Override public void initPipeline(final ChannelPipeline pipeline) throws Exception { final WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, null); final WebSocketClientProtocolHandler wsHandler = new WebSocketClientProtocolHandler(handshaker); pipeline.addFirst(new HttpClientCodec(), // new HttpObjectAggregator(65536), // wsHandler,// www .ja va2s.com // Fires channelActive() after handshake and removes self new WebSocketConnectedNotifier(), // BinaryWebSocketFrame <-> ByteBuf codec before user codecs new WebSocketBinaryCodec()); if (uri.getScheme().equalsIgnoreCase("wss") && pipeline.get(SslHandler.class) == null) { final SSLEngine sslEngine = SSLContext.getDefault().createSSLEngine(); sslEngine.setUseClientMode(true); pipeline.addFirst("ssl", new SslHandler(sslEngine)); } }
From source file:de.saxsys.synchronizefx.netty.websockets.WebsocketChannelInitializer.java
License:Open Source License
@Override public void addToPipeline(final ChannelPipeline pipeline) { pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); final WebSocketClientHandshaker handShaker = new WhiteSpaceInPathWebSocketClientHandshaker13(serverUri, WebSocketVersion.V13, PROTOCOL, false, createHttpHeaders(httpHeaders), Integer.MAX_VALUE); pipeline.addLast("websocket-protocol-handler", new WebSocketClientProtocolHandler(handShaker)); pipeline.addLast("websocket-frame-codec", new ByteBufToWebSocketFrameCodec()); }
From source file:divconq.api.HyperSession.java
License:Open Source License
public Channel allocateWsChannel(final ChannelHandler handler, OperationResult or) { final AtomicReference<Future<Channel>> sslready = new AtomicReference<>(); Bootstrap b = new Bootstrap(); b.group(Hub.instance.getEventLoopGroup()).option(ChannelOption.ALLOCATOR, Hub.instance.getBufferAllocator()) .channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w .j a va 2 s. co m*/ public void initChannel(SocketChannel ch) throws Exception { HttpHeaders customHeaders = new DefaultHttpHeaders(); customHeaders.add("x-DivConq-Mode", Hub.instance.getResources().getMode()); WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker( HyperSession.this.info.getUri(), WebSocketVersion.V13, null, false, customHeaders); ChannelPipeline pipeline = ch.pipeline(); if (HyperSession.this.info.isSecurel()) { SslHandler sh = new SslHandler(HyperSession.this.sslfac.getClientEngine()); sslready.set(sh.handshakeFuture()); pipeline.addLast("ssl", sh); } pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("ws-handler", new WebSocketClientProtocolHandler(handshaker)); pipeline.addLast("handler", handler); /* pipeline.addLast("handler", new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { System.out.println("read: " + msg); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { super.userEventTriggered(ctx, evt); Logger.debug("ue: " + evt); } }); */ } }); or.info("Web Client connecting"); try { // must wait here to make sure we don't release connectLock too soon // we want channel init (above) to complete before we try connect again ChannelFuture f = b.connect(this.info.getAddress()).sync(); if (!f.isSuccess()) { or.error(1, "Web Client unable to successfully connect: " + f.cause()); } // it has appeared that sometimes we "overshoot" the ssl handshake in code - to prevent // that lets wait for the handshake to be done for sure if (sslready.get() != null) { Future<Channel> sf = sslready.get().sync(); if (!sf.isSuccess()) { or.error(1, "Web Client unable to securely connect: " + sf.cause()); } } if (handler instanceof ClientHandler) ((ClientHandler) handler).waitConnect(); return f.channel(); } catch (InterruptedException x) { or.error(1, "Web Client interrupted while connecting: " + x); } catch (Exception x) { or.error(1, "Web Client unable to connect: " + x); } return null; }
From source file:io.reactivesocket.netty.websocket.client.ClientWebSocketDuplexConnection.java
License:Apache License
public static Publisher<ClientWebSocketDuplexConnection> create(URI uri, EventLoopGroup eventLoopGroup) { return s -> { WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()); CopyOnWriteArrayList<Observer<Frame>> subjects = new CopyOnWriteArrayList<>(); ReactiveSocketClientHandler clientHandler = new ReactiveSocketClientHandler(subjects); Bootstrap bootstrap = new Bootstrap(); ChannelFuture connect = bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w . j a v a 2 s . c o m*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker), clientHandler); } }).connect(uri.getHost(), uri.getPort()); connect.addListener(connectFuture -> { if (connectFuture.isSuccess()) { final Channel ch = connect.channel(); clientHandler.getHandshakePromise().addListener(handshakeFuture -> { if (handshakeFuture.isSuccess()) { s.onNext(new ClientWebSocketDuplexConnection(ch, subjects)); s.onComplete(); } else { s.onError(handshakeFuture.cause()); } }); } else { s.onError(connectFuture.cause()); } }); }; }
From source file:io.reactivesocket.transport.websocket.client.ClientWebSocketDuplexConnection.java
License:Apache License
public static Publisher<ClientWebSocketDuplexConnection> create(URI uri, EventLoopGroup eventLoopGroup) { return subscriber -> { WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()); CopyOnWriteArrayList<Observer<Frame>> subjects = new CopyOnWriteArrayList<>(); ReactiveSocketClientHandler clientHandler = new ReactiveSocketClientHandler(subjects); Bootstrap bootstrap = new Bootstrap(); ChannelFuture connect = bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override// ww w . j a va 2s . c o m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker), clientHandler); } }).connect(uri.getHost(), uri.getPort()); connect.addListener(connectFuture -> { subscriber.onSubscribe(EmptySubscription.INSTANCE); if (connectFuture.isSuccess()) { final Channel ch = connect.channel(); clientHandler.getHandshakePromise().addListener(handshakeFuture -> { if (handshakeFuture.isSuccess()) { subscriber.onNext(new ClientWebSocketDuplexConnection(ch, subjects)); subscriber.onComplete(); } else { subscriber.onError(handshakeFuture.cause()); } }); } else { subscriber.onError(connectFuture.cause()); } }); }; }
From source file:io.viewserver.network.netty.websocket.NettyWebSocketEndpoint.java
License:Apache License
@Override public IClient getClient(EventLoopGroup eventLoopGroup, ChannelHandler handler) { SslContext sslContext;/*w ww.ja v a2 s.co m*/ if (this.uri.getScheme().equals("wss")) { try { SslContextBuilder builder = SslContextBuilder.forClient(); if (bypassCertificateChecks || usingSelfSignedCertificate) { builder.trustManager(InsecureTrustManagerFactory.INSTANCE); } sslContext = builder.build(); } catch (SSLException e) { throw new RuntimeException(e); } } else { sslContext = null; } Bootstrap bootstrap = new Bootstrap(); WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslContext != null) { pipeline.addLast(sslContext.newHandler(ch.alloc(), uri.getHost(), uri.getPort())); } pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(1 << 30)); pipeline.addLast("websocket", new WebSocketClientProtocolHandler(handshaker)); pipeline.addLast(new ChannelInboundHandlerAdapter() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt == WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE) { ChannelPipeline pipeline = ctx.channel().pipeline(); pipeline.addAfter("websocket", "ws-decoder-xx", new MessageToMessageDecoder<BinaryWebSocketFrame>() { @Override protected void decode(ChannelHandlerContext ctx, BinaryWebSocketFrame msg, List<Object> out) throws Exception { out.add(msg.content().retain()); } }); pipeline.addAfter("websocket", "ws-encoder-xx", new MessageToMessageEncoder<ByteBuf>() { @Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { out.add(new BinaryWebSocketFrame(msg).retain()); } }); } super.userEventTriggered(ctx, evt); } }); pipeline.addLast("frameDecoder", new ChannelInboundHandlerAdapter()); pipeline.addLast("frameEncoder", new ChannelOutboundHandlerAdapter()); pipeline.addLast(handler); } }); return () -> bootstrap.connect(uri.getHost(), uri.getPort()); }
From source file:jlibs.wamp4j.netty.NettyClientEndpoint.java
License:Apache License
@Override public void connect(final URI uri, final ConnectListener listener, final String... subProtocols) { final SslContext sslContext; if ("wss".equals(uri.getScheme())) { try {/*from www. j a va 2s . c o m*/ if (sslSettings == null) { sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); } else { sslContext = SslContextBuilder.forClient().trustManager(sslSettings.trustCertChainFile) .keyManager(sslSettings.certificateFile, sslSettings.keyFile, sslSettings.keyPassword) .build(); } } catch (Throwable thr) { listener.onError(thr); return; } } else if ("ws".equals(uri.getScheme())) sslContext = null; else throw new IllegalArgumentException("invalid protocol: " + uri.getScheme()); final int port = uri.getPort() == -1 ? (sslContext == null ? 80 : 443) : uri.getPort(); Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.MAX_MESSAGES_PER_READ, 50000).option(ChannelOption.WRITE_SPIN_COUNT, 50000) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (sslContext != null) ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), uri.getHost(), port)); WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, Util.toString(subProtocols), false, new DefaultHttpHeaders()); ch.pipeline().addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker) { @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { super.exceptionCaught(ctx, cause); listener.onError(cause); } }, new HandshakeListener(handshaker, listener)); } }); bootstrap.connect(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { assert !future.channel().isOpen(); listener.onError(future.cause()); } } }); }
From source file:jlibs.wamp4j.netty.NettyWebSocketClient.java
License:Apache License
@Override public void connect(final URI uri, final ConnectListener listener, final String... subProtocols) { String protocol = uri.getScheme(); if (!protocol.equals("ws")) throw new IllegalArgumentException("invalid protocol: " + protocol); int port = uri.getPort(); if (port == -1) port = 80;// w ww .ja va 2 s.com Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, Util.toString(subProtocols), false, new DefaultHttpHeaders()); ch.pipeline().addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), new WebSocketClientProtocolHandler(handshaker), new HandshakeListener(handshaker, listener)); } }); bootstrap.connect(uri.getHost(), port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { assert !future.channel().isOpen(); listener.onError(future.cause()); } } }); }
From source file:org.eclipse.milo.opcua.stack.client.transport.websocket.OpcClientWebSocketChannelInitializer.java
License:Open Source License
@Override protected void initChannel(SocketChannel channel) throws Exception { String endpointUrl = config.getEndpoint().getEndpointUrl(); String scheme = EndpointUtil.getScheme(endpointUrl); TransportProfile transportProfile = TransportProfile.fromUri(config.getEndpoint().getTransportProfileUri()); String subprotocol;//w w w . j av a 2 s .c o m if (transportProfile == TransportProfile.WSS_UASC_UABINARY) { subprotocol = "opcua+cp"; } else if (transportProfile == TransportProfile.WSS_UAJSON) { subprotocol = "opcua+uajson"; } else { throw new UaException(StatusCodes.Bad_InternalError, "unexpected TransportProfile: " + transportProfile); } if ("opc.wss".equalsIgnoreCase(scheme)) { SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build(); channel.pipeline().addLast(sslContext.newHandler(channel.alloc())); } int maxMessageSize = config.getMessageLimits().getMaxMessageSize(); channel.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpObjectAggregator(maxMessageSize)); channel.pipeline() .addLast(new WebSocketClientProtocolHandler(WebSocketClientHandshakerFactory.newHandshaker( new URI(endpointUrl), WebSocketVersion.V13, subprotocol, true, new DefaultHttpHeaders(), config.getMessageLimits().getMaxChunkSize()))); channel.pipeline().addLast(new WebSocketFrameAggregator(config.getMessageLimits().getMaxMessageSize())); // OpcClientWebSocketFrameCodec adds UascClientAcknowledgeHandler when the WS upgrade is done. channel.pipeline().addLast(new OpcClientWebSocketBinaryFrameCodec(config, handshake)); }