List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(ChannelHandler... handlers);
From source file:cn.david.socks.SocksServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline p = socketChannel.pipeline(); p.addLast(new SocksInitRequestDecoder()); p.addLast(socksMessageEncoder);// w w w. ja va2 s .c o m p.addLast(socksServerHandler); }
From source file:cn.dennishucd.nettyhttpserver.PushServerInitializer.java
License:Apache License
@Override protected void initChannel(SocketChannel sc) throws Exception { ChannelPipeline p = sc.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpServerHandler()); }
From source file:cn.npt.net.websocket.WebSocketClient.java
License:Apache License
public static void main(String[] args) throws Exception { URI uri = new URI(URL); String scheme = uri.getScheme() == null ? "ws" : uri.getScheme(); final String host = uri.getHost() == null ? "192.168.20.71" : uri.getHost(); final int port; if (uri.getPort() == -1) { if ("ws".equalsIgnoreCase(scheme)) { port = 80;/*ww w. j av a2 s . c o m*/ } else if ("wss".equalsIgnoreCase(scheme)) { port = 443; } else { port = -1; } } else { port = uri.getPort(); } if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) { System.err.println("Only WS(S) is supported."); return; } final boolean ssl = "wss".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { // 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 BaseWebSocketClientHandler handler = new EchoWebSocketClientHandler( WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), host, port)); } p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler); } }); 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.wrappedBuffer(new byte[] { 8, 1, 8, 1 })); ch.writeAndFlush(frame); } else { WebSocketFrame frame = new TextWebSocketFrame(msg); ch.writeAndFlush(frame); } } } finally { group.shutdownGracefully(); } }
From source file:cn.npt.net.websocket.WebSocketServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }// ww w .ja v a 2 s. c o m pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WebSocketServerHandler(this.webSocketPath, this.ssl));//?handler //pipeline.addLast(bussinessLogicHandler); }
From source file:cn.scujcc.bug.bitcoinplatformandroid.util.socket.websocket.WebSocketBase.java
License:Apache License
private void connect() { try {//from ww w . j av a2s. c om final URI uri = new URI(url); if (uri == null) { return; } if (uri.getHost().contains("com")) { siteFlag = 1; } group = new NioEventLoopGroup(1); bootstrap = new Bootstrap(); final SslContext sslCtx = SslContext.newClientContext(); final WebSocketClientHandler handler = new WebSocketClientHandler( WebSocketClientHandshakerFactory.newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders(), Integer.MAX_VALUE), service, moniter); bootstrap.group(group).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), uri.getHost(), uri.getPort())); } p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler); } }); future = bootstrap.connect(uri.getHost(), uri.getPort()); future.addListener(new ChannelFutureListener() { public void operationComplete(final ChannelFuture future) throws Exception { } }); channel = future.sync().channel(); handler.handshakeFuture().sync(); this.setStatus(true); } catch (Exception e) { Log.e(TAG, "WebSocketClient start error " + e.getLocalizedMessage()); group.shutdownGracefully(); this.setStatus(false); } }
From source file:cn.wcl.test.netty.server.netty.HttpUploadServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }//from w w w .ja v a 2s .co m pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new HttpResponseEncoder()); // Remove the following line if you don't want automatic content // compression. // pipeline.addLast(new HttpContentCompressor()); pipeline.addLast(new ProtocalInboundHandler(applicationContext, needlogin)); }
From source file:co.paralleluniverse.comsat.webactors.netty.WebActorTest.java
License:Open Source License
@Before public void setUp() throws InterruptedException, IOException { System.out.println("Clearing sessions"); WebActorHandler.sessions.clear();/*from w ww . ja va2s . c o m*/ group = new NioEventLoopGroup(); final ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpRequestDecoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(HTTP_RESPONSE_ENCODER_KEY, new HttpResponseEncoder()); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new LoggingHandler(LogLevel.INFO)); pipeline.addLast(webActorHandlerCreatorInEffect.call()); } }); ch = b.bind(INET_PORT).sync(); AbstractEmbeddedServer.waitUrlAvailable("http://localhost:" + INET_PORT); System.err.println("Server is up"); }
From source file:co.rsk.rpc.netty.Web3WebSocketServer.java
License:Open Source License
public void start() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override// w w w. j av a 2 s.c o m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); p.addLast(new WebSocketServerProtocolHandler("/websocket")); p.addLast(jsonRpcHandler); p.addLast(web3ServerHandler); p.addLast(new Web3ResultWebSocketResponseHandler()); } }); webSocketChannel = b.bind(host, port); webSocketChannel.sync(); }
From source file:com.ahanda.techops.noty.clientTest.Client.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length == 1) { System.setProperty("PINT.conf", args[0]); }/*from ww w.j a va 2 s . c om*/ if (System.getProperty("PINT.conf") == null) throw new IllegalArgumentException(); JsonNode config = null; Config cf = Config.getInstance(); cf.setupConfig(); String scheme = "http"; String host = cf.getHttpHost(); int port = cf.getHttpPort(); if (port == -1) { port = 8080; } if (!"http".equalsIgnoreCase(scheme)) { l.warn("Only HTTP is supported."); return; } // Configure SSL context if necessary. final boolean ssl = "https".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpClientCodec()); // p.addLast( "decoder", new HttpResponseDecoder()); // p.addLast( "encoder", new HttpRequestEncoder()); // Remove the following line if you don't want automatic content decompression. // p.addLast(new HttpContentDecompressor()); // Uncomment the following line if you don't want to handle HttpContents. p.addLast(new HttpObjectAggregator(10485760)); p.addLast(new ClientHandler()); } }); // Make the connection attempt. Channel ch = b.connect(host, port).sync().channel(); ClientHandler client = new ClientHandler(); client.login(ch, ClientHandler.credential); // ClientHandler.pubEvent( ch, ClientHandler.event ); // Wait for the server to close the connection. ch.closeFuture().sync(); l.info("Closing Client side Connection !!!"); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); } }
From source file:com.alibaba.dubbo.qos.server.handler.QosProcessHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 1) { return;/*from w ww . java2s . c o m*/ } // read one byte to guess protocol final int magic = in.getByte(in.readerIndex()); ChannelPipeline p = ctx.pipeline(); p.addLast(new LocalHostPermitHandler(acceptForeignIp)); if (isHttp(magic)) { // no welcome output for http protocol if (welcomeFuture != null && welcomeFuture.isCancellable()) { welcomeFuture.cancel(false); } p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1048576)); p.addLast(new HttpProcessHandler()); p.remove(this); } else { p.addLast(new LineBasedFrameDecoder(2048)); p.addLast(new StringDecoder(CharsetUtil.UTF_8)); p.addLast(new StringEncoder(CharsetUtil.UTF_8)); p.addLast(new IdleStateHandler(0, 0, 5 * 60)); p.addLast(new TelnetProcessHandler()); p.remove(this); } }