📢📢📢📣📣📣
哈喽!大家好,我是【Bug 终结者】 ,【CSDN新星创作者】🏆,阿里云技术博主🏆,51CTO人气博主🏆,INfoQ写作专家🏆
一位上进心十足,拥有极强学习力的【Java领域博主】😜😜😜
🏅【Bug 终结者】博客的领域是【面向后端技术】的学习,未来会持续更新更多的【后端技术】以及【学习心得】。 偶尔会分享些前端基础知识,会更新实战项目,面向企业级开发应用! 🏅 如果有对【后端技术】、【前端领域】感兴趣的【小可爱】,欢迎关注【Bug 终结者】💞💞💞
?????? 感谢各位大可爱小可爱! ??????
Netty系列文章
Netty入门 – 什么是Netty?
一、需求说明
使用Netty实现群聊+私聊系统
- 编写一个Netty群聊系统,实现服务器端和客户端之间的数据简单通讯(非阻塞形式)
- 实现多人群聊
- 实现单人私聊
- 利用心跳检测机制监听客户端是否存在连接(是否存在读、写、读写操作)
- 服务器端:可以检测用户上线,离线,并且实现消息转发功能
- 客户端:通过channel可以无阻塞发送消息给其它所有在线用户,同时可以接受所有在线用户发送的消息(由服务器转发消息得到)
二、什么是心跳检测机制?
心跳检测机制就是在一定的时间范围内客户端与服务器之间没有发生读、写、读写操作,那么就认定客户端与服务器无连接,这样就节省了服务器的资源
??Netty实现心跳检测机制
服务器启动前添加前置处理器
pipeline.addLast(new IdleStateHandler(3, 5, 7, TimeUnit.SECONDS));
pipeline.addLast(new HeartbeatServerHandler());
IdleStateHandler是Netty 提供的处理空闲状态的处理器
参数说明
源码表示当前处理器类是表示多长时间内没有读、没有写、或者没有读写操作,就会触发IdleStateEvent事件 Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.
当IdleStateEvent事件 触发后, 就会传递给管道的下一个handler处理 通过调用(触发)handler的 userEventTiggered 在该方法中处理 当IdleStateEvent事件
HeartbeatServerHandler自定义心跳处理器
public class HeartbeatServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
String eventType = null;
switch (event.state()) {
case READER_IDLE:
eventType = "读空闲";
break;
case WRITER_IDLE:
eventType = "写空闲";
break;
case ALL_IDLE:
eventType = "读写空闲";
break;
}
System.out.println(ctx.channel().remoteAddress() + "发生超时事件,事件类型为:" + eventType);
System.out.println("服务器做相应处理");
}
}
心跳检测机制就是这样,简单来说,就是每隔一段时间去检测客户端是否与服务器连接,如果无连接,那么就断开,从而节省服务器的资源
三、需求分析
🚝多人群聊
利用map集合,Map<String, Channel> 里面存入当前在线的所有用户,继承 SimpleChannelInboundHandler 处理器 并在对应的处理器进行添加通道到map
然后实现处理器的channelRead0方法进行转发数据,这就简单的实现了多人群聊
🚝单人私聊
单人私聊与多人群聊类似,也是在channelRead0方法内进行判断是否为私聊用户,私聊用户输入#端口号#要发送的内容,即可简单检测到本次消息为私聊,并从map中取出对应的key,拿出key对应的channel,进行转发,即可完成私聊
接受消息,其它用户不会看到此私聊消息
🚝服务器检测用户上线、离线
服务器端检测用户当前的状态,实现对应的方法进行相应的提示即可
-
实现handlerAdded检测某个用户加入聊天, -
实现channelActive表示channel处于活跃状态,即上线 -
实现channelInactive表示channel处于非活跃状态,即离线, -
实现 handlerRemoved 表示离线
四、效果图
五、核心源码
GroupChatServer服务器端代码
package com.wanshi.netty.groupchat;
import com.wanshi.netty.heartbeat.HeartbeatServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import java.util.concurrent.TimeUnit;
public class GroupChatServer {
private int port;
public GroupChatServer(int port) {
this.port = port;
}
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
try {
bootstrap.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 socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
pipeline.addLast(new GroupChatServerHandler());
pipeline.addLast(new IdleStateHandler(3, 5, 7, TimeUnit.SECONDS));
pipeline.addLast(new HeartbeatServerHandler());
}
});
System.out.println("netty 服务器启动");
ChannelFuture future = bootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
GroupChatServer groupChatServer = new GroupChatServer(7000);
groupChatServer.run();
}
}
GroupChatServerHandler 服务器自定义handler
package com.wanshi.netty.groupchat;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {
private static Map<String,Channel> allChannels = new HashMap<String,Channel>();
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String currentDate = sdf.format(new Date());
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
allChannels.forEach((k, ch) ->{
ch.writeAndFlush(currentDate+" \n [客户端]" + channel.remoteAddress() + "加入聊天\n");
});
String key = channel.remoteAddress().toString().split(":")[1];
allChannels.put(key, channel);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
allChannels.forEach((k, ch) ->{
ch.writeAndFlush(currentDate+" \n [客户端]" + channel.remoteAddress() + "离线\n");
});
System.out.println("当前在线人数:" + allChannels.size());
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
Channel channel = channelHandlerContext.channel();
if(s.contains("#")){
String id = s.split("#")[1];
String body = s.split("#")[2];
Channel userChannel = allChannels.get(id);
String key = channel.remoteAddress().toString().split(":")[1];
userChannel.writeAndFlush(currentDate+"\n "+key+"【私聊】 [用户] "+id+" 说 : "+body);
return;
}
allChannels.forEach((k, ch) -> {
if (channel != ch) {
ch.writeAndFlush(currentDate + " \n [客户端]" + channel.remoteAddress() + ":" + s + "\n");
} else {
channel.writeAndFlush(currentDate + " \n [我]:" + s + "\n");
}
});
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(currentDate + " -- " + ctx.channel().remoteAddress() + "上线~");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
String key = channel.remoteAddress().toString().split(":")[1];
allChannels.remove(key);
System.out.println(currentDate + " -- " + ctx.channel().remoteAddress() + "离线");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
自定义心跳处理器 – HeartbeatServerHandler
package com.wanshi.netty.heartbeat;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;
public class HeartbeatServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
String eventType = null;
switch (event.state()) {
case READER_IDLE:
eventType = "读空闲";
break;
case WRITER_IDLE:
eventType = "写空闲";
break;
case ALL_IDLE:
eventType = "读写空闲";
break;
}
System.out.println(ctx.channel().remoteAddress() + "发生超时事件,事件类型为:" + eventType);
System.out.println("服务器做相应处理");
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println("发生异常!");
}
}
GroupChatClient 客户端
package com.wanshi.netty.groupchat;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.util.Scanner;
public class GroupChatClient {
private final String host;
public final int port;
public GroupChatClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run () {
EventLoopGroup eventExecutors = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.group(eventExecutors)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.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.hasNext()) {
String msg = scanner.nextLine();
channel.writeAndFlush(msg+"\r\n");
}
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
} finally {
eventExecutors.shutdownGracefully();
}
}
public static void main(String[] args) {
GroupChatClient groupChatClient = new GroupChatClient("127.0.0.1", 7000);
groupChatClient.run();
}
}
GroupChatClientHandler 客户端自定义处理器Handler
package com.wanshi.netty.groupchat;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class GroupChatServerHandler extends SimpleChannelInboundHandler<String> {
private static Map<String,Channel> allChannels = new HashMap<String,Channel>();
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private String currentDate = sdf.format(new Date());
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
allChannels.forEach((k, ch) ->{
ch.writeAndFlush(currentDate+" \n [客户端]" + channel.remoteAddress() + "加入聊天\n");
});
String key = channel.remoteAddress().toString().split(":")[1];
allChannels.put(key, channel);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
allChannels.forEach((k, ch) ->{
ch.writeAndFlush(currentDate+" \n [客户端]" + channel.remoteAddress() + "离线\n");
});
System.out.println("当前在线人数:" + allChannels.size());
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
Channel channel = channelHandlerContext.channel();
if(s.contains("#")){
String id = s.split("#")[1];
String body = s.split("#")[2];
Channel userChannel = allChannels.get(id);
String key = channel.remoteAddress().toString().split(":")[1];
userChannel.writeAndFlush(currentDate+"\n "+key+"【私聊】 [用户] "+id+" 说 : "+body);
return;
}
allChannels.forEach((k, ch) -> {
if (channel != ch) {
ch.writeAndFlush(currentDate + " \n [客户端]" + channel.remoteAddress() + ":" + s + "\n");
} else {
channel.writeAndFlush(currentDate + " \n [我]:" + s + "\n");
}
});
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(currentDate + " -- " + ctx.channel().remoteAddress() + "上线~");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
String key = channel.remoteAddress().toString().split(":")[1];
allChannels.remove(key);
System.out.println(currentDate + " -- " + ctx.channel().remoteAddress() + "离线");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
往期精彩热文回顾
🚀 Netty入门 – 什么是Netty? 🚀 如何免费使用阿里云服务器?【一篇文章教会你,真香】 🚀 如何使用Git SVN工具 – TortoiseGit(小乌龟)将本地项目上传至GitEE?【超详细教程】 🚀 前后端分离系列 – SpringBoot + Spring Security + Vue 实现用户认证 SpringSecurity如此简单
🚀 Postman测试工具调试接口详细教程【向后端发送Json数据并接收返回的Json结果】
🚀 Java面向对象 — 吃货联盟订餐系统(完整版)
?小结
以上就是【Bug 终结者】对Netty非阻塞网络编程简单的理解,小编认为唯有代码实践,才可提升自己的技术,手不要懒,多敲,本案例完美的体现了Netty非阻塞式网络编程的模式,方便,快捷,代码略微有点多,但滤清思路,一步步来,总会慢慢理解的,加油,希望本文对你有帮助~
如果这篇【文章】有帮助到你,希望可以给【Bug 终结者】点个赞👍,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注?????? 【Bug 终结者】??????,我将会给你带来巨大的【收获与惊喜】💝💝💝!
|