简单记录一下实现的整体框架,具体细节在实际生产中再细化就可以了。

第一步 引入netty依赖

SpringBoot的其他必要的依赖像Mybatis、Lombok这些都是老生常谈了 就不在这里放了

       <dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.85.Final</version>
</dependency>

第二步 接下来就是准备工作。

消息服务类(核心代码) 聊天服务的功能就是靠这个类的start()函数来启动的 绑定端口8087 之后可以通socket协议访问这个端口来执行通讯

import com.bxt.demo.im.handler.WebSocketHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* @Description: 即时通讯服务类
* @author: bhw
* @date: 2023年09月27日 13:44
*/
@Slf4j
public class IMServer {
  // 用来存放连入服务器的用户集合
public static final Map<String, Channel> USERS = new ConcurrentHashMap<>(1024);
  // 用来存放创建的群聊连接
public static final ChannelGroup GROUP = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); public static void start() throws InterruptedException {
log.info("IM服务开始启动");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup(); // 绑定端口
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
// 添加http编码解码器
pipeline.addLast(new HttpServerCodec())
//支持大数据流
.addLast(new ChunkedWriteHandler())
// 对http消息做聚合操作 FullHttpRequest FullHttpResponse
.addLast(new HttpObjectAggregator(1024*64))
//支持websocket
.addLast(new WebSocketServerProtocolHandler("/"))
.addLast(new WebSocketHandler());
}
}); ChannelFuture future = bootstrap.bind(8087).sync();
log.info("服务器启动开始监听端口: {}", 8087);
future.channel().closeFuture().sync();
//关闭主线程组
bossGroup.shutdownGracefully();
//关闭工作线程组
workGroup.shutdownGracefully();
} }

创建聊天消息实体类

/**
* @Description: 聊天消息对象 可以自行根据实际业务扩展
* @author: seizedays
*/
@Data
public class ChatMessage extends IMCommand {
//消息类型
private Integer type;
//消息目标对象
private String target;
//消息内容
private String content; }

连接类型枚举类,暂时定义为建立连接、发送消息和加入群组三种状态码

@AllArgsConstructor
@Getter
public enum CommandType { //建立连接
CONNECT(10001),
//发送消息
CHAT(10002),
//加入群聊
JOIN_GROUP(10003),
ERROR(-1)
; private Integer code; public static CommandType match(Integer code){
for (CommandType value : CommandType.values()) {
if (value.code.equals(code)){
return value;
}
}
return ERROR;
} }

命令动作为聊天的时候 消息类型又划分为私聊和群聊两种 枚举类如下:

@AllArgsConstructor
@Getter
public enum MessageType { //私聊
PRIVATE(1),
//群聊
GROUP(2),
ERROR(-1)
;
private Integer type; public static MessageType match(Integer code){
for (MessageType value : MessageType.values()) {
if (value.type.equals(code)){
return value;
}
}
return ERROR;
} }

创建连接请求的拦截器

import com.alibaba.fastjson2.JSON;
import com.bxt.common.vo.Result;
import com.bxt.demo.im.cmd.IMCommand;
import com.bxt.demo.im.server.IMServer;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; /**
* @Description: 用户连接到服务端的拦截器
* @author: bhw
* @date: 2023年09月27日 14:28
*/
public class ConnectionHandler {
public static void execute(ChannelHandlerContext ctx, IMCommand command) {
if (IMServer.USERS.containsKey(command.getNickName())) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error(command.getNickName() + "已经在线,不能重复连接"))));
ctx.channel().disconnect();
return;
} IMServer.USERS.put(command.getNickName(), ctx.channel()); ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.success("系统消息:" + command.getNickName() + "与服务端连接成功")))); ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.success(JSON.toJSONString(IMServer.USERS.keySet())))));
}
}

加入群组功能的拦截器

/**
* @Description: 加入群聊拦截器
* @author: bhw
* @date: 2023年09月27日 15:07
*/
public class JoinGroupHandler {
public static void execute(ChannelHandlerContext ctx) {
try {
IMServer.GROUP.add(ctx.channel());
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.success("加入系统默认群组成功!"))));
} catch (Exception e) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("消息内容异常"))));
} }
}

发送聊天到指定对象的功能拦截器

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.bxt.common.vo.Result;
import com.bxt.demo.im.cmd.ChatMessage;
import com.bxt.demo.im.cmd.MessageType;
import com.bxt.demo.im.server.IMServer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import java.util.Objects; /**
* @Description: 聊天拦截器
* @author: bhw
* @date: 2023年09月27日 15:07
*/
public class ChatHandler {
public static void execute(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
try {
ChatMessage message = JSON.parseObject(frame.text(), ChatMessage.class);
MessageType msgType = MessageType.match(message.getType()); if (msgType.equals(MessageType.PRIVATE)) {
if (StringUtils.isBlank(message.getTarget())){
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("系统消息:消息发送失败,请选择消息发送对象"))));
return;
}
Channel channel = IMServer.USERS.get(message.getTarget());
if (Objects.isNull(channel) || !channel.isActive()){
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("系统消息:消息发送失败,对方不在线"))));
IMServer.USERS.remove(message.getTarget());
return;
}
channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.success("私聊消息(" + message.getTarget() + "):" + message.getContent())))); } else if (msgType.equals(MessageType.GROUP)) {
IMServer.GROUP.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.success("群消息:发送者(" + message.getNickName() + "):" + message.getContent()))));
}else {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("系统消息:不支持的消息类型"))));
} } catch (Exception e) {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("消息内容异常"))));
} }
}

最后是websocket拦截器 接收到客户端的指令后选择对应的拦截器实现相应的功能:

import com.alibaba.fastjson2.JSON;
import com.bxt.common.vo.Result;
import com.bxt.demo.im.cmd.CommandType;
import com.bxt.demo.im.cmd.IMCommand;
import com.bxt.demo.im.server.IMServer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j; /**
* @Description: websocket拦截器
* @author: bhw
* @date: 2023年09月27日 13:59
*/
@Slf4j
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> { @Override
protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame frame) {
System.out.println(frame.text());
try {
IMCommand command = JSON.parseObject(frame.text(), IMCommand.class);
CommandType cmdType = CommandType.match(command.getCode());
if (cmdType.equals(CommandType.CONNECT)){
ConnectionHandler.execute(ctx, command);
} else if (cmdType.equals(CommandType.CHAT)) {
ChatHandler.execute(ctx,frame);
} else if (cmdType.equals(CommandType.JOIN_GROUP)) {
JoinGroupHandler.execute(ctx);
} else {
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error("不支持的code"))));
}
}catch (Exception e){
ctx.channel().writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(Result.error(e.getMessage()))));
} } @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// 当连接断开时被调用
Channel channel = ctx.channel();
// 从 USERS Map 中移除对应的 Channel
removeUser(channel);
super.channelInactive(ctx);
} private void removeUser(Channel channel) {
// 遍历 USERS Map,找到并移除对应的 Channel
IMServer.USERS.entrySet().removeIf(entry -> entry.getValue() == channel);
}
}

第三步 启动服务

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
// 启动IM服务
try {
IMServer.start();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
} }

现在 客户端通过socket协议访问8087端口即可实现基本的聊天室功能了!

基于SpringBoot+Netty实现即时通讯(IM)功能的更多相关文章

  1. 基于Android 平台简易即时通讯的研究与设计[转]

    摘要:论文简单介绍Android 平台的特性,主要阐述了基于Android 平台简易即时通讯(IM)的作用和功能以及实现方法.(复杂的通讯如引入视频音频等可以考虑AnyChat SDK~)关键词:An ...

  2. 使用 HTML5 webSocket API实现即时通讯的功能

    project下载地址:http://download.csdn.net/detail/wangshuxuncom/6430191 说明: 本project用于展示怎样使用 HTML5 webSock ...

  3. easy-im:一款基于netty的即时通讯系统

    介绍 easy-im是面向开发者的一款轻量级.开箱即用的即时通讯系统,帮助开发者快速搭建消息推送等功能. 基于easy-im,你可以快速实现以下功能: + 聊天软件 + IoT消息推送 基本用法 项目 ...

  4. websocket和基于swoole的简易即时通讯

    这里描述个基于swoole的websocket 匿名群聊 UI <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  5. 基于SpringBoot+Netty实现一个自己的推送服务系统

    目标 实现一个WebSocket服务中心,支持水平扩展 技术栈 SpringBoot.Netty.JDK8.MySQL.Redis.RabbitMQ.MyBatis-Plus 环境搭建 主要功能点说明 ...

  6. Flutter高仿微信项目开源-具即时通讯IM功能

    项目地址:https://github.com/fluttercandies/wechat_flutter wechat_flutter  Flutter版本微信 效果图: 下载体验(Android) ...

  7. [Python]实现XMPP协议即时通讯发送消息功能

    #-*- coding: utf-8 -*- __author__ = 'tsbc' import xmpp import time #注意帐号信息,必须加@域名格式 from_user = 'che ...

  8. 即时通讯(IM-instant messager)

    即时通讯又叫实时通讯,简单来说就是两个及以上的人使用网络进行文字.文件.语音和视频的交流. 首先,进行网络进行通信,肯定需要网络协议,即时通讯专用的协议就是xmpp.xmpp协议要传递的消息类型是xm ...

  9. openfire+asmack搭建的安卓即时通讯(一) 15.4.7

    最进开始做一些android的项目,除了一个新闻客户端的搭建,还需要一个实现一个即时通讯的功能,参考了很多大神成型的实例,了解到operfire+asmack是搭建简易即时通讯比较方便,所以就写了这篇 ...

  10. openfire+asmack搭建的安卓即时通讯(三) 15.4.9

    (能用得上话的话求点赞=-=,我表达不好的话跟我说哦) 上一次我们拿到了服务器端的组数据和用户信息,这就可以为我们日后使用好友系统打下基础了! 但是光是拿到了这些东西我们怎么能够满足呢?我们一个即时通 ...

随机推荐

  1. SQL Server中的NULL值处理:判断与解决方案

    摘要: 在SQL Server数据库中,NULL是表示缺少数据或未知值的特殊标记.处理NULL值是SQL开发人员经常遇到的问题之一.本文将介绍SQL Server中判断和处理NULL值的不同方法,以及 ...

  2. kibana基本操作

    kibana基本应用 一.简介 ​ Kibana是一个开源的分析与可视化平台,设计出来用于和Elasticsearch一起使用的.你可以用kibana搜索.查看存放在Elasticsearch中的数据 ...

  3. 自己动手实现rpc框架(二) 实现集群间rpc通信

    自己动手实现rpc框架(二) 实现集群间rpc通信 1. 集群间rpc通信 上一篇博客中MyRpc框架实现了基本的点对点rpc通信功能.而在这篇博客中我们需要实现MyRpc的集群间rpc通信功能. 自 ...

  4. HCL实验6:静态路由

    拓扑图 步骤: 连线,路由器与路由器通过S端口连接 配置好PC 配置路由器端口IP 配置路由器的下一跳地址(静态路由) 详细步骤 连线情况可见拓扑图 配置好PC 端口IP R1 [R1]int g0/ ...

  5. .net core提示502.5错误

    最近给WindowsServer2012服务器部署.Net Core项目,部署后一直显示502.5错误,具体如下: 网上找了一大堆解决办法都行不通,最后在stackoverflow中找到说是缺少一个补 ...

  6. Hexo博客Next主题bilibili视频Markdown插入文章

    问题及需求 B站视频无广告有弹幕,非常简洁,经常看B站视频,在文章引用B站的视频 在不用插件的情况下用官方的iframe方式引入视频,默认的方式导入视频屏幕会很小 一般我们都是自己改width和hei ...

  7. eclipse在主题商城下载安装黑色主题

    Eclipse配置黑色主题方法: 1. 借用国外一个Elipse主题网站分享的主题配置文件来配置一个黑色的主题. 主题网址 2. 在这个网站下载自己喜欢的主题,单击主题进入下载页面,建议大家选择EPF ...

  8. zabbix 中 net.if.out 值来源及persecond的计算

    使用脚本记录每秒的net.if.out值,与zabbix中的lastdata值做对比,发现对不上. #!/bin/bash dev=eth0 get_dev_net_speed() { dev_inf ...

  9. 2021-11-30 wpf的mvvm绑定2

    主页页面代码 <Grid> <TextBox x:Name="First" Width="80" Height="20" ...

  10. CSS: 绝对定位fixed

    属性介绍 元素会被移出正常文档流,并不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置.元素的位置在屏幕滚动时不会改变.打印时,元素会出现在的每页的固定位置.fi ...