netty吧 关注:1,480贴子:756
  • 5回复贴,共1

channelActive 没主动推送

只看楼主收藏回复

代码在评论


IP属地:广东1楼2017-12-11 17:45回复
    package netty.protobuf;
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelFuture;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.ChannelOption;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.codec.protobuf.ProtobufDecoder;
    import io.netty.handler.codec.protobuf.ProtobufEncoder;
    import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
    import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    public class SubReqServer {
    public void bind(int port) throws Exception{
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try{
    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>() {
    public void initChannel(SocketChannel ch){
    ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
    ch.pipeline().addLast(new ProtobufDecoder(
    SubscribeReqProto.SubscribeReq.getDefaultInstance()));
    ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
    ch.pipeline().addLast(new ProtobufEncoder());
    ch.pipeline().addLast(new SubReqServerHandler());
    }
    });
    ChannelFuture f =b.bind(port).sync();
    f.channel().closeFuture().sync();
    }finally {
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
    }
    }
    public static void main(String[] args) throws Exception{
    int port = 8080;
    if(args!=null && args.length>0){
    try{
    port=Integer.valueOf(args[0]);
    }catch (NumberFormatException e){
    }
    }
    new SubReqServer().bind(port);
    }
    }


    IP属地:广东2楼2017-12-11 17:46
    回复
      package netty.protobuf;
      import io.netty.channel.ChannelHandlerAdapter;
      import io.netty.channel.ChannelHandlerContext;
      public class SubReqServerHandler extends ChannelHandlerAdapter {
      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq) msg;
      if ("Young".equalsIgnoreCase(req.getUserName())) {
      System.out.println("Service accept client subscribe req : [" + req.toString() + "]");
      ctx.writeAndFlush(resp(req.getSubReqID()));
      }
      }
      private SubscribeRespProto.SubscribeResp resp(int subReqID){
      SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
      builder.setSubReqID(subReqID);
      builder.setRespCode(0);
      builder.setDesc("Netty book order succeed, 3 days later, sent to the designated address");
      return builder.build();
      }
      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
      cause.printStackTrace();
      ctx.close();
      }
      }


      IP属地:广东3楼2017-12-11 17:46
      回复
        package netty.protobuf;
        import io.netty.channel.ChannelHandlerAdapter;
        import io.netty.channel.ChannelHandlerContext;
        import java.util.ArrayList;
        import java.util.List;
        public class SubReqClientHandler extends ChannelHandlerAdapter{
        public SubReqClientHandler(){
        }
        public void channelActive(ChannelHandlerContext ctx){
        for(int i=0;i<10;i++){
        ctx.write(subReq(i));
        }
        ctx.flush();
        }
        private SubscribeReqProto.SubscribeReq subReq(int i) {
        SubscribeReqProto.SubscribeReq.Builder builder = SubscribeReqProto.SubscribeReq.newBuilder();
        builder.setSubReqID(i);
        builder.setUserName("Young");
        builder.setProductName("Netty Book Protobuf");
        List<String> address = new ArrayList <>();
        address.add("NanJing LiuLiTai");
        address.add("BeiJing LiuLiChang");
        address.add("ShenZhen HongShuLin");
        builder.addAllAddress(address);
        return builder.build();
        }
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception{
        System.out.println("Receive server response : [" + msg + "]");
        }
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception{
        ctx.flush();
        }
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
        cause.printStackTrace();
        ctx.close();
        }
        }


        IP属地:广东4楼2017-12-11 17:47
        回复
          package netty.protobuf;
          import io.netty.bootstrap.Bootstrap;
          import io.netty.channel.ChannelFuture;
          import io.netty.channel.ChannelInitializer;
          import io.netty.channel.ChannelOption;
          import io.netty.channel.EventLoopGroup;
          import io.netty.channel.nio.NioEventLoopGroup;
          import io.netty.channel.socket.SocketChannel;
          import io.netty.channel.socket.nio.NioSocketChannel;
          import io.netty.handler.codec.protobuf.ProtobufDecoder;
          import io.netty.handler.codec.protobuf.ProtobufEncoder;
          import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
          import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
          public class SubReqClient {
          public void connect(int port, String host) throws Exception{
          EventLoopGroup group = new NioEventLoopGroup();
          try{
          Bootstrap b =new Bootstrap();
          b.group(group).channel(NioSocketChannel.class)
          .option(ChannelOption.TCP_NODELAY, true)
          .handler(new ChannelInitializer<SocketChannel>() {
          public void initChannel(SocketChannel ch)throws Exception{
          ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
          ch.pipeline().addLast(new ProtobufDecoder(SubscribeRespProto.SubscribeResp.getDefaultInstance()));
          ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
          ch.pipeline().addLast(new ProtobufEncoder());
          ch.pipeline().addLast(new SubReqClientHandler());
          }
          });
          ChannelFuture f = b.connect(host,port).sync();
          f.channel().closeFuture().sync();
          }finally {
          group.shutdownGracefully();
          }
          }
          public static void main(String[] args) throws Exception{
          int port = 8080;
          if(args!=null && args.length>0){
          try{
          port=Integer.valueOf(args[0]);
          }catch (NumberFormatException e){
          }
          }
          new SubReqClient().connect(port,"127.0.0.1");
          }
          }


          IP属地:广东5楼2017-12-11 17:47
          回复
            就是不知道为啥不主动推送,鸟大的请指教


            IP属地:广东6楼2017-12-11 17:48
            回复