Java使用netty

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

  1. 首先maven的配置
        <build>
            <plugins>
                <!--【必用插件】用于设置项目jdk版本-->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>8</source>
                        <target>8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    
        <dependencies>
            <dependency>
                <groupId>io.netty</groupId>
                <artifactId>netty-all</artifactId>
                <version>4.1.20.Final</version>
            </dependency>
            <dependency>
                <groupId>com.google.protobuf</groupId>
                <artifactId>protobuf-java</artifactId>
                <version>3.2.0</version>
            </dependency>
    
        </dependencies>

  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<SocketChannel>() {
                        @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<SocketChannel>() {
                        @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();
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值