• netty同时支持socket和http


    项目需要使用netty做中转服务器,同时支持两种不同协议的客户端,经过几天查询资料终于找到合适的方案了,同时感谢Netty权威指南及论坛问答,开始贴代码

    客户端1==》socket

     1 public class Bluetooth implements Runnable {
     2     //蓝牙
     3  
     4     private int port;
     5     @Override
     6     public void run() {
     7         System.out.println("--------进入蓝牙---------");
     8         EventLoopGroup bossGroup = new NioEventLoopGroup();
     9         EventLoopGroup workGroup = new NioEventLoopGroup();
    10         try {
    11             ServerBootstrap b = new ServerBootstrap();
    12             b.group(bossGroup, workGroup);
    13             b.channel(NioServerSocketChannel.class);
    14             b.childHandler(new ChannelInitializer<SocketChannel>() {
    15                 @Override
    16                 public void initChannel(SocketChannel ch) throws Exception {
    17                     System.out.println("chhhh"+ch.id());
    18                             // 注册handler
    19                             /*ch.pipeline().addLast("http-codec", new HttpServerCodec());
    20                             ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
    21                             ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());*/
    22                     ch.pipeline().addLast(new SimpleServerHandler());
    23                 }
    24             });
    25 //            b.childHandler(new ChannelFilter());
    26             System.out.println("平台监听开启....");
    27             Channel ch = b.bind(5500).sync().channel();
    28             ch.closeFuture().sync();
    29  
    30         } catch (Exception e) {
    31             e.printStackTrace();
    32         }finally{
    33             //优雅的退出程序
    34             bossGroup.shutdownGracefully();
    35             workGroup.shutdownGracefully();
    36         }
    37     }
    38 

    客户端2==》http

    public class Myweb implements Runnable {
        //Myweb
     
        private int port;
        @Override
        public void run() {
            System.out.println("--------进入web---------");
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            EventLoopGroup workGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workGroup);
                b.channel(NioServerSocketChannel.class);
                b.childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel e) throws Exception {
                        System.out.println("chhhh"+e.id());
                        e.pipeline().addLast("http-codec", new HttpServerCodec());
                        e.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
                        e.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        e.pipeline().addLast(new SimpleServerHandler());
                    }
                });
    //            b.childHandler(new ChannelFilter());
                System.out.println("平台监听开启....");
                Channel ch = b.bind(8888).sync().channel();
                ch.closeFuture().sync();
     
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                //优雅的退出程序
                bossGroup.shutdownGracefully();
                workGroup.shutdownGracefully();
            }
        }
    }

    main==>开启两个监听线程

    public class main {
        private int port;
     
        public main(int port) {
            this.port = port;
        }
     
        public void run() throws Exception {
     
        }
     
        public static void main(String[] args) throws Exception {
    //        new main(5500).run();
            Bluetooth bluetooth = new Bluetooth();
            Myweb myweb = new Myweb();
     
            Thread th1 = new Thread(bluetooth);
            Thread th2 = new Thread(myweb);
            th1.start();
            th2.start();
     
        }
    }
    Handler代码就不贴了,网上很多,主要是通过多线程分别使用不同编解码器,
    对不同客户端的协议进行解析。同时将Chnnel通道保存在Map集合中,两个线程可共享这个Chnnel。











    参考:https://blog.csdn.net/lmianhuatang/article/details/79675790
  • 相关阅读:
    ZRender实现粒子网格动画实战
    线段树专题—ZOJ1610 Count the Colors
    LeetCode Recover Binary Search Tree
    Android跨进程訪问(AIDL服务)
    刘下记录:ImageView.scaleType 属性全解析(含视频)
    myeclipse解决JSP文件里script背景颜色的调整
    hdu 5381 The sum of gcd(线段树+gcd)
    newlisp HTTP Basic Authentication
    codeforces 132C Logo Turtle--- dp dfs
    python代码风格-PEP8
  • 原文地址:https://www.cnblogs.com/panchanggui/p/9812153.html
Copyright © 2020-2023  润新知