Netty 多人聊天服务器:使用 StringDecoder 和 StringEncoder 实现
import io.netty.bootstrap.ServerBootstrap; // 导入Netty的ServerBootstrap类,用于启动服务器 import io.netty.channel.ChannelFuture; // 导入Netty的ChannelFuture类,用于异步操作的结果通知 import io.netty.channel.ChannelHandlerContext; // 导入Netty的ChannelHandlerContext类,用于处理网络事件 import io.netty.channel.ChannelInitializer; // 导入Netty的ChannelInitializer类,用于初始化Channel import io.netty.channel.ChannelOption; // 导入Netty的ChannelOption类,用于设置Channel的选项 import io.netty.channel.EventLoopGroup; // 导入Netty的EventLoopGroup类,用于处理事件的线程组 import io.netty.channel.nio.NioEventLoopGroup; // 导入Netty的NioEventLoopGroup类,用于处理I/O事件的线程组 import io.netty.channel.socket.SocketChannel; // 导入Netty的SocketChannel类,用于处理TCP连接 import io.netty.channel.socket.nio.NioServerSocketChannel; // 导入Netty的NioServerSocketChannel类,用于创建服务器端的Channel import io.netty.handler.codec.string.StringDecoder; // 导入Netty的StringDecoder类,用于将字节流解码为字符串 import io.netty.handler.codec.string.StringEncoder; // 导入Netty的StringEncoder类,用于将字符串编码为字节流 import io.netty.util.concurrent.DefaultEventExecutorGroup; // 导入Netty的DefaultEventExecutorGroup类,用于处理耗时操作 import io.netty.util.concurrent.EventExecutorGroup; // 导入Netty的EventExecutorGroup类,用于处理耗时操作
import java.util.concurrent.ConcurrentHashMap; // 导入Java的ConcurrentHashMap类,用于存储用户和对应的ChannelHandlerContext
public class MultiChatServer { private final int port; // 端口号 private final ConcurrentHashMap<String, ChannelHandlerContext> userMap = new ConcurrentHashMap<>(); // 存储用户和对应的ChannelHandlerContext的ConcurrentHashMap private final EventExecutorGroup group = new DefaultEventExecutorGroup(16); // 用于处理耗时操作的EventExecutorGroup
public MultiChatServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // 创建boss线程组,用于处理连接请求
EventLoopGroup workerGroup = new NioEventLoopGroup(); // 创建worker线程组,用于处理I/O事件
try {
ServerBootstrap b = new ServerBootstrap(); // 创建ServerBootstrap实例
b.group(bossGroup, workerGroup) // 设置EventLoopGroup
.channel(NioServerSocketChannel.class) // 指定使用NioServerSocketChannel作为服务器端的Channel
.childHandler(new ChannelInitializer<SocketChannel>() { // 设置ChannelInitializer
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringDecoder(), new StringEncoder(), new MultiChatServerHandler(userMap, group)); // 添加StringDecoder、StringEncoder和MultiChatServerHandler到Channel的pipeline中
}
})
.option(ChannelOption.SO_BACKLOG, 128) // 设置服务器端的tcp参数
.childOption(ChannelOption.SO_KEEPALIVE, true); // 设置客户端的tcp参数
ChannelFuture f = b.bind(port).sync(); // 绑定端口并启动服务器
System.out.println('Server started on port ' + port);
f.channel().closeFuture().sync(); // 关闭Channel并等待关闭完成
} finally {
workerGroup.shutdownGracefully(); // 优雅地关闭worker线程组
bossGroup.shutdownGracefully(); // 优雅地关闭boss线程组
}
}
public static void main(String[] args) throws Exception {
int port = 8080; // 设置服务器端口号
new MultiChatServer(port).run(); // 创建MultiChatServer实例并启动服务器
}
}
原文地址: https://www.cveoy.top/t/topic/f3Be 著作权归作者所有。请勿转载和采集!