Netty 处理Tcp Http Udp

说明

netty5 监听 tcp端口,http端口,udp端口 分别处理消息

1.启动类方法

启动netty 监听端口事件

 public void init(int httpport,int tcpport,int udpport) throws Exception {
 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
        try{
        //http和tcp设置
            ServerBootstrap bootstrap=new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup);
            bootstrap.channel(NioServerSocketChannel.class);
            bootstrap.option(ChannelOption.SO_BACKLOG,1024);
        //UDP 设置
            Bootstrap Bootstrap2 = new Bootstrap();//udp不能使用ServerBootstrap
            Bootstrap2.group(workerGroup);
            Bootstrap2 .channel(NioDatagramChannel.class);//设置UDP通道
            Bootstrap2 .option(ChannelOption.SO_BROADCAST, true);// 支持广播
            Bootstrap2 .option(ChannelOption.SO_BACKLOG, 128);
            Bootstrap2 .option(ChannelOption.SO_RCVBUF, 1024 * 1024);// 设置UDP读缓冲区为1M
            Bootstrap2 .option(ChannelOption.SO_SNDBUF, 1024 * 1024);// 设置UDP写缓冲区为1M

            List ports = Arrays.asList(httpport,tcpport,udpport);
            Collection channels = new ArrayList(ports.size());
            for (int pot : ports) {
                Channel serverChannel=null;
                if (pot==udpport){
                    Bootstrap2 .handler(udpServerInitializer);//初始化处理器
                    serverChannel = Bootstrap2.bind(pot).sync().channel();
                }else if (pot==httpport){
                    bootstrap.childHandler(httpServerInitializer);
                    serverChannel = bootstrap.bind(pot).sync().channel();
                }else if (pot==tcpport){
                    bootstrap.childHandler(tcpServerInitializer);
                   serverChannel = bootstrap.bind(pot).sync().channel();
                }
                channels.add(serverChannel);
            }
            for (Channel ch : channels) {
                ch.closeFuture().sync();
            }
      //  future.channel().closeFuture().sync();
    }catch(Exception e){
        System.out.println("Server服务端报异常---------"+e.getMessage());
    }finally {
           bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
}

2.http处理方法格式化化协议处理

 
   @Autowired
    private HttpServerHandler httpServerHandler;
  @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        /* 处理http处理 */
        if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("http.port")){
            socketChannel.pipeline().addLast(new HttpServerCodec());/*HTTP 服务的解码器*/
            socketChannel.pipeline().addLast(new HttpObjectAggregator(2048));/*HTTP 消息的合并处理*/
            socketChannel.pipeline().addLast(httpServerHandler);
            //处理tcp请求
        }else{
            log.info("请求端口出错:"+socketChannel.localAddress().getPort());
           
        }
    }

3.tcp处理方法格式化协议处理

  @Autowired
    private TcpServerHandler tcpServerHandler;
    @Autowired
    private TcpHeartBeatServerHandler tcpHeartBeatServerHandler;
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        /* 处理tcp处理 */
         if (socketChannel.localAddress().getPort()== ConfigProperties.getInt("tcp.port")){
            socketChannel.pipeline().addLast(new ByteArrayDecoder());
            socketChannel.pipeline().addLast(new ByteArrayEncoder());
            socketChannel.pipeline().addLast(tcpServerHandler);
            //心跳包
            socketChannel.pipeline().addLast(new IdleStateHandler(300,0,0, TimeUnit.SECONDS));
            socketChannel.pipeline().addLast("hearbeata",tcpHeartBeatServerHandler); //心跳事件
        }else{
        }
       
    }

4.udp 处理方法 格式化协议处理

    @Autowired
    private  UdpChatServerHandler udpChatServerHandler;
    @Autowired
    private  UdpHeartBeatServerHandler udpHeartBeatServerHandler;

    @Override
    protected void initChannel(NioDatagramChannel nioDatagramChannel) throws Exception {
        ChannelPipeline pipeline = nioDatagramChannel.pipeline();
        pipeline.addLast("handler",udpChatServerHandler);//消息处理器
       // pipeline.addLast("ackHandler", new UdpAckServerHandler());//ack处理器

        pipeline.addLast("timeout", new IdleStateHandler(180, 0, 0, TimeUnit.SECONDS));// //此两项为添加心跳机制,60秒查看一次在线的客户端channel是否空闲
        pipeline.addLast("hearbeat",udpHeartBeatServerHandler);// 心跳处理handler
    }

5.注意

框架采用netty5集成spring,mybatis

在协议处理中需要写自己处理消息的方法处理等业务

你可能感兴趣的:(Netty,开发框架,物联网)