09-09-netty完善修改

This commit is contained in:
2025-09-09 18:47:48 +08:00
parent a52b9078a0
commit 724ebf8dbd

View File

@ -140,7 +140,7 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
historyLambdaQueryWrapper.eq(ChatHistory::getIsRead, "1"); historyLambdaQueryWrapper.eq(ChatHistory::getIsRead, "1");
List<ChatHistory> list = chatHistoryService.list(historyLambdaQueryWrapper); List<ChatHistory> list = chatHistoryService.list(historyLambdaQueryWrapper);
if (list != null && !list.isEmpty()) { if (list != null && !list.isEmpty()) {
roomCounts.put(chatGroup.getId().toString(), list.size()); roomCounts.put(loginUser.getUserId()+"+"+chatGroup.getId().toString(), list.size());
} }
//在遍历的同时寻找是否有系统消息房间 //在遍历的同时寻找是否有系统消息房间
if (chatGroup.getMembers().contains("system")){ if (chatGroup.getMembers().contains("system")){
@ -263,8 +263,14 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(id.toString()); List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(id.toString());
//如果满足则说明用户在线 //如果满足则说明用户在线
if (channelHandlerContexts != null && !channelHandlerContexts.isEmpty()) { if (channelHandlerContexts != null && !channelHandlerContexts.isEmpty()) {
//所有房间的未读消息数 //构建所有房间的未读消息数
jsonObject.put("countValue", JsonUtils.toJsonString(userRoomCountMap)); HashMap<String,Object> temp = new HashMap<>();
userRoomCountMap.forEachKey(0, (value) -> {
if (value.contains(id.toString())){
temp.put(value,userRoomCountMap.get(value));
}
});
jsonObject.put("countValue", JsonUtils.toJsonString(temp));
//给每个通道发送对应消息 //给每个通道发送对应消息
for (ChannelHandlerContext handlerContext : channelHandlerContexts) { for (ChannelHandlerContext handlerContext : channelHandlerContexts) {
sendMessage(handlerContext, jsonObject.toString()); sendMessage(handlerContext, jsonObject.toString());
@ -342,19 +348,27 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
//通过userId进行发送消息 //通过userId进行发送消息
@Transactional @Transactional
public void sendSystemMessageToUser(Long userId, String message){ public void sendSystemMessageToUser(Long userId, String message){
// /现在已用type0 1 3 4用于服务端发送消息到系统消息房间 // /现在已用type0 1 3 4用于服务端发送消息到系统消息房间(暂定为0)
// { // {
// "type":"4", // "type":"4",
// "message": "testing" // "message": "testing"
// } // }
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "4"); jsonObject.put("type", "0");
jsonObject.put("message", message); jsonObject.put("message", message);
//通过userId拿到该用户所有通道实例 //通过userId拿到该用户所有通道实例
List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(userId.toString()); List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(userId.toString());
if (channelHandlerContexts != null && !channelHandlerContexts.isEmpty()) { if (channelHandlerContexts != null && !channelHandlerContexts.isEmpty()) {
//给每个通道发送对应消息 //给每个通道发送对应消息
for (ChannelHandlerContext handlerContext : channelHandlerContexts) { for (ChannelHandlerContext handlerContext : channelHandlerContexts) {
//同时发送所有未读消息给前端进行渲染
HashMap<String,Object> temp = new HashMap<>();
userRoomCountMap.forEachKey(0, (value) -> {
if (value.contains(userId.toString())){
temp.put(value,userRoomCountMap.get(value));
}
});
jsonObject.put("countValue", temp);
sendMessage(handlerContext, jsonObject.toString()); sendMessage(handlerContext, jsonObject.toString());
} }
} }