netty是高性能的socket框架,作为游戏的服务器端是个不错的选择
- 首先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>
- 到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();
}
}