| public class GroupChatServer { |
| |
| private int port; |
| |
| public GroupChatServer(int port) { |
| this.port = port; |
| } |
| |
| |
| public void run() throws Exception{ |
| |
| |
| EventLoopGroup bossGroup = new NioEventLoopGroup(1); |
| EventLoopGroup workerGroup = new NioEventLoopGroup(); |
| |
| try { |
| ServerBootstrap b = new ServerBootstrap(); |
| |
| b.group(bossGroup, workerGroup) |
| .channel(NioServerSocketChannel.class) |
| .option(ChannelOption.SO_BACKLOG, 128) |
| .childOption(ChannelOption.SO_KEEPALIVE, true) |
| .childHandler(new ChannelInitializer<SocketChannel>() { |
| |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| |
| |
| ChannelPipeline pipeline = ch.pipeline(); |
| |
| pipeline.addLast("decoder", new StringDecoder()); |
| |
| pipeline.addLast("encoder", new StringEncoder()); |
| |
| pipeline.addLast(new GroupChatServerHandler()); |
| |
| } |
| }); |
| |
| System.out.println("netty 服务器启动"); |
| ChannelFuture channelFuture = b.bind(port).sync(); |
| |
| |
| channelFuture.channel().closeFuture().sync(); |
| }finally { |
| bossGroup.shutdownGracefully(); |
| workerGroup.shutdownGracefully(); |
| } |
| |
| } |
| |
| public static void main(String[] args) throws Exception { |
| |
| new GroupChatServer(7000).run(); |
| } |
| } |
| public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> { |
| |
| |
| |
| |
| |
| |
| private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); |
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| |
| |
| |
| |
| @Override |
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
| Channel channel = ctx.channel(); |
| |
| |
| |
| |
| |
| channelGroup.writeAndFlush("[客户端]" + channel.remoteAddress() + " 加入聊天" + sdf.format(new java.util.Date()) + " \n"); |
| channelGroup.add(channel); |
| |
| } |
| |
| |
| @Override |
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
| |
| Channel channel = ctx.channel(); |
| channelGroup.writeAndFlush("[客户端]" + channel.remoteAddress() + " 离开了\n"); |
| System.out.println("channelGroup size" + channelGroup.size()); |
| } |
| |
| |
| @Override |
| public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| System.out.println(ctx.channel().remoteAddress() + " 上线了~"); |
| } |
| |
| |
| @Override |
| public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| System.out.println(ctx.channel().remoteAddress() + " 离线了~"); |
| } |
| |
| |
| @Override |
| protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { |
| |
| |
| Channel channel = ctx.channel(); |
| |
| |
| channelGroup.forEach(ch -> { |
| if(channel != ch) { |
| ch.writeAndFlush("[客户]" + channel.remoteAddress() + " 发送了消息" + msg + "\n"); |
| }else { |
| ch.writeAndFlush("[自己]发送了消息" + msg + "\n"); |
| } |
| }); |
| } |
| |
| @Override |
| public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| |
| ctx.close(); |
| } |
| } |
| public class GroupChatClient { |
| |
| |
| private final String host; |
| private final int port; |
| |
| public GroupChatClient(String host, int port) { |
| this.host = host; |
| this.port = port; |
| } |
| |
| public void run() throws Exception{ |
| EventLoopGroup group = new NioEventLoopGroup(); |
| |
| try { |
| |
| Bootstrap bootstrap = new Bootstrap() |
| .group(group) |
| .channel(NioSocketChannel.class) |
| .handler(new ChannelInitializer<SocketChannel>() { |
| |
| @Override |
| protected void initChannel(SocketChannel ch) throws Exception { |
| |
| |
| ChannelPipeline pipeline = ch.pipeline(); |
| |
| pipeline.addLast("decoder", new StringDecoder()); |
| pipeline.addLast("encoder", new StringEncoder()); |
| |
| pipeline.addLast(new GroupChatClientHandler()); |
| } |
| }); |
| |
| ChannelFuture channelFuture = bootstrap.connect(host, port).sync(); |
| |
| Channel channel = channelFuture.channel(); |
| System.out.println("-------" + channel.localAddress()+ "--------"); |
| |
| Scanner scanner = new Scanner(System.in); |
| while (scanner.hasNextLine()) { |
| String msg = scanner.nextLine(); |
| |
| channel.writeAndFlush(msg + "\r\n"); |
| } |
| }finally { |
| group.shutdownGracefully(); |
| } |
| } |
| |
| public static void main(String[] args) throws Exception { |
| new GroupChatClient("127.0.0.1", 7000).run(); |
| } |
| } |
| public class GroupChatClientHandler extends SimpleChannelInboundHandler<String> { |
| @Override |
| protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { |
| System.out.println(msg.trim()); |
| } |
| } |
| |
| netty 服务器启动 |
| /127.0.0.1:59224 上线了~ |
| |
| |
| -------/127.0.0.1:59224-------- |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
2021-08-09 错误处理
2021-08-09 spring boot文件上传