gpt4 book ai didi

java - 网络设计【使用Netty】

转载 作者:行者123 更新时间:2023-12-01 12:44:45 25 4
gpt4 key购买 nike

我正在开发一个多人游戏项目,但我对如何设置它有点困惑。主要是我对Netty框架不熟悉。

每个玩家应该有自己的管道来处理数据包吗?或者应该只有一个管道来处理所有入站数据包?

如果一个玩家应该拥有自己的数据包,我如何让该玩家成为管道的所有者?

目前这是我的服务器代码

public static void main(String[] params) throws Exception
{
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try
{
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<SocketChannel>()
{
@Override
public void initChannel(SocketChannel channel) throws Exception
{
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
pipeline.addLast("PacketHandler", new SinglePacketPipe());
System.err.println("Connection Established - Pipes constructed..");
}
});

ChannelFuture future = bootstrap.bind(SERVER_PORT).sync();
System.err.println("Server initialized..");
// When the server socket is closed, destroy the future.
future.channel().closeFuture().sync();
}
finally
{
// Destroy all executor groups
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}

最佳答案

根据定义,Netty 中的 Channel 总是有自己的管道。也就是说,如果有 1000 个连接,则有 1000 个管道。让它们在这些管道中具有相同的处理程序集,或者让其中一些具有不同的管道配置,完全取决于您。

请注意,管道是动态可配置的。您可以决定在 initChannel() 或处理程序的 channelRegistered()(甚至在任何处理程序方法中)添加/删除哪些处理程序。

关于java - 网络设计【使用Netty】,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24809801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com