09-09-netty完善修改
This commit is contained in:
@ -140,7 +140,7 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
|
||||
historyLambdaQueryWrapper.eq(ChatHistory::getIsRead, "1");
|
||||
List<ChatHistory> list = chatHistoryService.list(historyLambdaQueryWrapper);
|
||||
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")){
|
||||
@ -263,8 +263,14 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
|
||||
List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(id.toString());
|
||||
//如果满足则说明用户在线
|
||||
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) {
|
||||
sendMessage(handlerContext, jsonObject.toString());
|
||||
@ -342,19 +348,27 @@ public class ChatServerHandler extends SimpleChannelInboundHandler<TextWebSocket
|
||||
//通过userId进行发送消息
|
||||
@Transactional
|
||||
public void sendSystemMessageToUser(Long userId, String message){
|
||||
// /现在已用type:0 1 3 4用于服务端发送消息到系统消息房间
|
||||
// /现在已用type:0 1 3 4用于服务端发送消息到系统消息房间(暂定为0)
|
||||
// {
|
||||
// "type":"4",
|
||||
// "message": "testing"
|
||||
// }
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("type", "4");
|
||||
jsonObject.put("type", "0");
|
||||
jsonObject.put("message", message);
|
||||
//通过userId拿到该用户所有通道实例
|
||||
List<ChannelHandlerContext> channelHandlerContexts = userChannelMap.get(userId.toString());
|
||||
if (channelHandlerContexts != null && !channelHandlerContexts.isEmpty()) {
|
||||
//给每个通道发送对应消息
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user