package com.ljm.boot.nettywebsocket.netty;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljm.boot.nettywebsocket.model.SocketMessage;
import com.ljm.boot.nettywebsocket.repository.UserGroupRepository;
import com.ljm.boot.nettywebsocket.util.SpringUtil;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author Dominick Li
* @createTime 2020/2/28 13:07
* @description
**/
public class TestWebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
private final Logger logger = LoggerFactory.getLogger(TestWebSocketHandler.class);
/**
* 存储已经登录用户的channel对象
*/
public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
/**
* 存储用户id和用户的channelId绑定
*/
public static ConcurrentHashMap<Integer, ChannelId> userMap = new ConcurrentHashMap<>();
/**
* 用于存储群聊房间号和群聊成员的channel信息
*/
public static ConcurrentHashMap<Integer, ChannelGroup> groupMap = new ConcurrentHashMap<>();
/**
* 获取用户拥有的群聊id号
*/
UserGroupRepository userGroupRepositor = SpringUtil.getBean(UserGroupRepository.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.info("与客户端建立连接,通道开启!");
//添加到channelGroup通道组
channelGroup.add(ctx.channel());
ctx.channel().id();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("与客户端断开连接,通道关闭!");
//添加到channelGroup 通道组
channelGroup.remove(ctx.channel());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//首次连接是FullHttpRequest,把用户id和对应的channel对象存储起来
if (null != msg && msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
String uri = request.uri();
Integer userId = getUrlParams(uri);
userMap.put(getUrlParams(uri), ctx.channel().id());
logger.info("登录的用户id是:{}", userId);
//第1次登录,需要查询下当前用户是否加入过群,加入过群,则放入对应的群聊里
List<Integer> groupIds = userGroupRepositor.findGroupIdByUserId(userId);
ChannelGroup cGroup = null;
//查询用户拥有的组是否已经创建了
for (Integer groupId : groupIds) {
cGroup = groupMap.get(groupId);
//如果群聊管理对象没有创建
if (cGroup == null) {
//构建一个channelGroup群聊管理对象然后放入groupMap中
cGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
groupMap.put(groupId, cGroup);
}
//把用户放到群聊管理对象里去
cGroup.add(ctx.channel());
}
//如果url包含参数,需要处理
if (uri.contains("?")) {
String newUri = uri.substring(0, uri.indexOf("?"));
request.setUri(newUri);
}
} else if (msg instanceof TextWebSocketFrame) {
//正常的TEXT消息类型
TextWebSocketFrame frame = (TextWebSocketFrame) msg;
logger.info("客户端收到服务器数据:{}", frame.text());
SocketMessage socketMessage = JSON.parseObject(frame.text(), SocketMessage.class);
//处理群聊任务
if ("group".equals(socketMessage.getMessageType())) {
//推送群聊信息
groupMap.get(socketMessage.getChatId()).writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(socketMessage)));
} else {
//处理私聊的任务,如果对方也在线,则推送消息
ChannelId channelId = userMap.get(socketMessage.getChatId());
if (channelId != null) {
Channel ct = channelGroup.find(channelId);
if (ct != null) {
ct.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(socketMessage)));
}
}
}
}
super.channelRead(ctx, msg);
}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
}
private static Integer getUrlParams(String url) {
if (!url.contains("=")) {
return null;
}
String userId = url.substring(url.indexOf("=") + 1);
return Integer.parseInt(userId);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
使用Netty加websocket实现在线聊天功能].zip
共280个文件
gif:153个
png:47个
js:31个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 25 浏览量
2023-08-26
10:55:22
上传
评论
收藏 3.14MB ZIP 举报
温馨提示
基于springboot框架实现一些常用的功能
资源推荐
资源详情
资源评论
收起资源包目录
使用Netty加websocket实现在线聊天功能].zip (280个子文件)
mvnw.cmd 6KB
amazeui.min.css 249KB
layui.css 91KB
layer.css 14KB
layer.css 14KB
main.css 12KB
login.css 11KB
layui.mobile.css 10KB
laydate.css 7KB
code.css 1KB
iconfont.eot 41KB
34.gif 13KB
38.gif 10KB
59.gif 10KB
22.gif 10KB
17.gif 8KB
18.gif 8KB
24.gif 8KB
11.gif 8KB
13.gif 7KB
31.gif 7KB
16.gif 7KB
39.gif 6KB
64.gif 6KB
46.gif 6KB
63.gif 6KB
50.gif 6KB
loading-0.gif 6KB
loading-0.gif 6KB
30.gif 6KB
4.gif 6KB
1.gif 5KB
42.gif 5KB
71.gif 5KB
21.gif 5KB
43.gif 5KB
20.gif 5KB
8.gif 5KB
28.gif 5KB
29.gif 5KB
42.gif 5KB
5.gif 5KB
70.gif 4KB
5.gif 4KB
32.gif 4KB
17.gif 4KB
27.gif 4KB
40.gif 4KB
9.gif 4KB
44.gif 4KB
11.gif 4KB
14.gif 4KB
8.gif 4KB
3.gif 4KB
25.gif 4KB
7.gif 4KB
10.gif 4KB
44.gif 4KB
23.gif 4KB
48.gif 4KB
34.gif 4KB
41.gif 4KB
38.gif 4KB
65.gif 3KB
32.gif 3KB
68.gif 3KB
45.gif 3KB
9.gif 3KB
7.gif 3KB
27.gif 3KB
39.gif 3KB
12.gif 3KB
26.gif 3KB
60.gif 3KB
6.gif 3KB
2.gif 3KB
40.gif 3KB
25.gif 3KB
16.gif 3KB
47.gif 3KB
21.gif 3KB
19.gif 3KB
66.gif 3KB
18.gif 3KB
46.gif 3KB
10.gif 3KB
28.gif 3KB
51.gif 3KB
57.gif 3KB
67.gif 3KB
0.gif 3KB
48.gif 3KB
43.gif 3KB
60.gif 3KB
26.gif 3KB
30.gif 2KB
74.gif 2KB
61.gif 2KB
33.gif 2KB
24.gif 2KB
共 280 条
- 1
- 2
- 3
资源评论
sjx_alo
- 粉丝: 1w+
- 资源: 1213
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功