List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.guowl.websocket.client.WebSocketClientRunner.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*from w ww .ja v a 2 s. c o m*/ // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. // If you change it to V00, ping is not supported and remember to change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); final String protocol = uri.getScheme(); int defaultPort; ChannelInitializer<SocketChannel> initializer; // Normal WebSocket if ("ws".equals(protocol)) { initializer = new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-codec", new HttpClientCodec()) .addLast("aggregator", new HttpObjectAggregator(8192)) .addLast("ws-handler", handler); } }; defaultPort = 80; // Secure WebSocket } else if ("wss".equals(protocol)) { initializer = new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { SSLEngine engine = WebSocketSslClientContextFactory.getContext().createSSLEngine(); engine.setUseClientMode(true); ch.pipeline().addFirst("ssl", new SslHandler(engine)) .addLast("http-codec", new HttpClientCodec()) .addLast("aggregator", new HttpObjectAggregator(8192)) .addLast("ws-handler", handler); } }; defaultPort = 443; } else { throw new IllegalArgumentException("Unsupported protocol: " + protocol); } Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(initializer); int port = uri.getPort(); // If no port was specified, we'll try the default port: https://tools.ietf.org/html/rfc6455#section-1.7 if (uri.getPort() == -1) { port = defaultPort; } Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while (true) { String msg = console.readLine(); if (msg == null) { break; } else if ("bye".equals(msg.toLowerCase())) { ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); break; } else if ("ping".equals(msg.toLowerCase())) { WebSocketFrame frame = new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 8, 1, 8, 1 })); ch.writeAndFlush(frame); } else { WebSocketFrame frame = new TextWebSocketFrame(msg); ch.writeAndFlush(frame); } } } finally { group.shutdownGracefully(); } }
From source file:com.guowl.websocket.stream.client.StreamClientRunner.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*from ww w . j a v a 2s . c o m*/ // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 // or V00. // If you change it to V00, ping is not supported and remember to // change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the // pipeline. final StreamClientHandler handler = new StreamClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); final String protocol = uri.getScheme(); int defaultPort; ChannelInitializer<SocketChannel> initializer; // Normal WebSocket if ("ws".equals(protocol)) { initializer = new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-codec", new HttpClientCodec()) .addLast("aggregator", new HttpObjectAggregator(8192)) .addLast("ws-handler", handler); } }; defaultPort = 80; // Secure WebSocket } else { throw new IllegalArgumentException("Unsupported protocol: " + protocol); } Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(initializer); int port = uri.getPort(); // If no port was specified, we'll try the default port: if (uri.getPort() == -1) { port = defaultPort; } Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); ByteBuf dataBuf = ch.alloc().buffer(); dataBuf.writeBytes("start".getBytes()); ch.writeAndFlush(new BinaryWebSocketFrame(false, 0, dataBuf)); ch.writeAndFlush(new ContinuationWebSocketFrame(true, 0, "end")); Thread.sleep(1000 * 60 * 60); } finally { group.shutdownGracefully(); } }
From source file:com.gxkj.demo.netty.discard.DiscardServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w. j a va 2s . com*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DiscardServerHandler()); } }); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.gxkj.demo.netty.echo.EchoClient.java
License:Apache License
public void run() throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {//www .j a v a 2 s .c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( //new LoggingHandler(LogLevel.INFO), new EchoClientHandler(firstMessageSize)); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.gxkj.demo.netty.echo.EchoServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w . jav a2 s . co m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( //new LoggingHandler(LogLevel.INFO), new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.gxkj.demo.netty.uptime.UptimeClient.java
License:Apache License
Bootstrap configureBootstrap(Bootstrap b, EventLoopGroup g) { b.group(g).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(new ChannelInitializer<SocketChannel>() { @Override//from w w w.j av a 2 s .c o m public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler); } }); return b; }
From source file:com.hazelcast.openshift.TunnelClient.java
License:Open Source License
public ServerBootstrap createBootstrap(int localPort) throws Exception { System.out.println(/*from w w w. ja va2 s . co m*/ "Creating clientside plain-socket: (" + localPort + ") => (" + httpHost + ":" + httpPort + ")"); return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup()) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new TunnelServerConnector(getWorkerGroup(), httpHost, httpPort, httpSsl)); } }); }
From source file:com.hazelcast.openshift.TunnelClientAcceptor.java
License:Open Source License
protected Bootstrap createBootstrap(Channel socket) { return new Bootstrap().channel(NioSocketChannel.class).group(workerGroup) .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { @Override// w w w. j a v a 2 s. c o m protected void initChannel(SocketChannel channel) throws Exception { System.out.println("Configure plain-socket: (" + socket + ") => (" + forwardHost + ":" + forwardPort + ")"); ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new ProxyForwardHandler(socket)); } }); }
From source file:com.hazelcast.openshift.TunnelServer.java
License:Open Source License
@Override protected ServerBootstrap createBootstrap(int localPort) throws Exception { SslContext sslContext;/* w ww . j a v a2 s . c o m*/ if (!ssl) { sslContext = null; } else { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } System.out.println("Creating serverside http-socket: (" + localPort + ") => (" + forwardHost + ":" + forwardPort + ")"); return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup()) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast("ssl", sslContext.newHandler(channel.alloc())); } pipeline.addLast("http-codec", new HttpServerCodec()); pipeline.addLast(new TunnelClientAcceptor(getWorkerGroup(), forwardHost, forwardPort)); } }); }
From source file:com.hazelcast.openshift.TunnelServerConnector.java
License:Open Source License
protected Bootstrap createBootstrap(Channel socket, Promise promise) throws Exception { SslContext sslContext;/*w w w .j av a 2 s . c om*/ if (!ssl) { sslContext = null; } else { sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } return new Bootstrap().channel(NioSocketChannel.class).group(workerGroup) .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { System.out.println( "Configure plain-socket: (" + socket + ") => (" + httpHost + ":" + httpPort + ")"); ChannelPipeline pipeline = channel.pipeline(); if (sslContext != null) { pipeline.addLast("ssl", sslContext.newHandler(channel.alloc())); } pipeline.addLast("http-codec", new HttpClientCodec()); pipeline.addLast(new TunnelServerAcceptor(socket, promise)); } }); }