Java使用netty

netty是高性能的socket框架,作为游戏的服务器端是个不错的选择

  1. 首先maven的配置
        
            
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.8.1
                    
                        8
                        8
                        UTF-8
                    
                
            
        
    
    
        
            
                io.netty
                netty-all
                4.1.20.Final
            
            
                com.google.protobuf
                protobuf-java
                3.2.0
            
    
        

  2. 到protobuf官网下载protobuf编译器3.2.0版本(编译器版本与protobuf依赖版本对应)

https://github.com/protocolbuffers/protobuf/releases

客户端模板

    public void connect(String ip, int port) {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
//使用protoBuf传输用以下四个
                            pipeline.addLast("Encoder", new ProtobufVarint32LengthFieldPrepender());
                            pipeline.addLast("Decoder", new ProtobufVarint32FrameDecoder());
                            pipeline.addLast("protobufEncoder", new ProtobufEncoder());
                            pipeline.addLast("protobufDecoder", new ProtobufDecoder(MessagePOJO.Message.getDefaultInstance()));
//使用Json字符串传输用以下四个
                            pipeline.addLast("Encoder",new LengthFieldPrepender(2));
                            pipeline.addLast("Decoder",new LengthFieldBasedFrameDecoder(65535,0,2,0,2));
                            pipeline.addLast("StringEncoder",new StringEncoder());
                            pipeline.addLast("StringDecoder",new StringDecoder());

                            //此类继承于ChannelInboundHandlerAdapter
                            pipeline.addLast(new SimpleClientHandler());
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(ip, port).sync();
            if (channelFuture.isSuccess()) System.out.println("连接成功!!!!");
            else System.out.println("连接失败");
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

服务器模板 

public void startServer() {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast("intDecoder", new ProtobufVarint32FrameDecoder());
                            pipeline.addLast("intEncoder", new ProtobufVarint32LengthFieldPrepender());
                            pipeline.addLast("protobufDecoder", new ProtobufDecoder(MessagePOJO.Message.getDefaultInstance()));
                            //此类继承于ChannelInboundHandlerAdapter
                            pipeline.addLast(new SimpleServerHandler());
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            if (channelFuture.isSuccess()) System.out.println("服务器开启成功!!!!");
            else System.out.println("服务器开启失败");
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

你可能感兴趣的:(Netty和DotNetty,java,maven,开发语言)