Netty学习——基于netty实现简单的客户端聊天小程序
效果图,聊天程序展示 (TCP编程实现)
后端代码:
package com.dawa.netty.chatexample; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; /** * @Title: MyChatServer * @Author: 大娃 * @Date: 2019/11/26 09:44 * @Description: 客户端 */ public class MyChatServer { public static void main(String[] args) throws InterruptedException { //定义两个 循环组 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { //服务器端 启动类 ServerBootstrap serverBootstrap = new ServerBootstrap(); //启动类装载两个 循环组 serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new MyChatServerInitializer());//初始化器 ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); channelFuture.channel().closeFuture().sync(); }finally { //关闭循环组 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
package com.dawa.netty.chatexample; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.group.ChannelGroup; import io.netty.channel.group.DefaultChannelGroup; import io.netty.util.concurrent.GlobalEventExecutor; /** * @Title: MyChatServerHandler * @Author: 大娃 * @Date: 2019/11/26 09:54 * @Description: 消息的广播 * ABC 三个客户端,分别建立服务器的链接 * A,第一个用户,没必要通知 * B,A,B和服务器的控制台都提示 B已经上线, * C,ABC和服务器端,都是C上线 * * * 自己想的:根据生命周期,当注册的时候,提示已经上线,当注销的时候,提示已经下线。 * 当发送一个消息到服务器端的时候,服务器端广播一下。 判断如果是自己的IP的话,提示自己。不是的话,显示IP * * netty都是通过响应的回调方法,来进行实现的 * 1.当连接建立好的时候,就代表有一个客户端和服务端建立起连接了、。 handlerAdded */ public class MyChatServerHandler extends SimpleChannelInboundHandler<String> { //用来保存一个个的channel对象的。 private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); //收到任何一个消息之后,的回调函数 @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 { cause.printStackTrace(); ctx.close(); } /** * 1.当连接建立好的时候,就代表有一个客户端和服务端建立起连接了。 handlerAdded * @param ctx * @throws Exception */ @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { //1.建立起连接 Channel channel = ctx.channel(); //2.进行广播。 channelGroup.writeAndFlush("【服务器】:" + channel.remoteAddress() + "加入\n"); //3.添加到组 channelGroup.add(channel); //服务器端是不是需要将所有已经建立起连接的channel 保存起来? channelGroup } /** * 2.当离开的时候 * @param ctx * @throws Exception */ @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); channelGroup.writeAndFlush("【服务器】:" + channel.remoteAddress() + "离开 \n "); //无需手工的移除, 会自动将断掉的链接移除 。 现在先不管,以后可以看看 // channelGroup.remove(channel); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); System.out.println(channel.remoteAddress()+" 上线"); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); System.out.println(channel.remoteAddress()+" 下线"); } }
package com.dawa.netty.chatexample; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; /** * @Title: MyChatServerInitializer * @Author: 大娃 * @Date: 2019/11/26 09:46 * @Description: */ public class MyChatServerInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //第一个解码器:自带的。 根据分隔符来进行解析 pipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); //Handler pipeline.addLast(new MyChatServerHandler()); } }
客户端代码: (别人的电脑都可以模拟终端,启动这个客户端即可连接服务器)
package com.dawa.netty.chatexample; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import java.io.BufferedReader; import java.io.InputStreamReader; /** * @Title: MyChatClient * @Author: 大娃 * @Date: 2019/11/26 14:39 * @Description: */ public class MyChatClient { public static void main(String[] args) throws Exception { //循环组,一个就够用了。客户端 EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); try { //客户端使用的是bootstrap, Bootstrap bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(new MyChatClientInitializer()); //注意此处,使用的是connect,不是使用的bind Channel channel = bootstrap.connect("localhost", 8899).sync().channel(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); for (; ; ) { channel.writeAndFlush(br.readLine()+ "\r\n"); } } finally { eventLoopGroup.shutdownGracefully(); } } }
package com.dawa.netty.chatexample; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; /** * @Title: MyChatClientInitializer * @Author: 大娃 * @Date: 2019/11/26 14:42 * @Description: */ public class MyChatClientInitializer extends ChannelInitializer<SocketChannel> { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //添加Handler ChannelPipeline channelPipeline = ch.pipeline(); channelPipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter())); channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); //添加自己的Handler pipeline.addLast(new MyChatClientHandler()); } }
package com.dawa.netty.chatexample; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; /** * @Title: MyChatClientHandler * @Author: 大娃 * @Date: 2019/11/26 14:45 * @Description: */ public class MyChatClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println(msg); } }
原文地址:https://www.cnblogs.com/bigbaby/p/11969512.html
时间: 2024-10-01 04:55:45