src/main/java/com/xcong/excoin/netty/bean/ChatRequest.java
@@ -1,58 +1,31 @@ package com.xcong.excoin.netty.bean; import java.io.Serializable; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * {"data": {"fromMemberId": 443, "isSelf": 1, "memberId": 443, "msg": "w shi shei ", "msgType": 1, "targetId": 444}, "type": 2} */ @Data public class ChatRequest implements Serializable { private static final long serialVersionUID = 3079080008111369136L; private String to; private Long fromMemberId; private String from; private Long targetId; private Long memberId; private Integer msgType; private String content; private String msg; private Integer isSelf; private Integer cmd; public Integer getCmd() { return cmd; } public void setCmd(Integer cmd) { this.cmd = cmd; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public Integer getMsgType() { return msgType; } public void setMsgType(Integer msgType) { this.msgType = msgType; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } private Long timestamp; } src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
@@ -79,16 +79,22 @@ ChatRequest chat = JSONObject.parseObject(chatStr, ChatRequest.class); Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); channel.writeAndFlush(NettyTools.webSocketJson(ResponseBean.ok(chat))); ResponseBean res = ResponseBean.ok(chat); res.setType(2); channel.writeAndFlush(NettyTools.webSocketJson(res)); // 判断是否在线 Channel targetChannel = ChannelManager.findWsChannel(Long.parseLong(chat.getTo())); Channel targetChannel = ChannelManager.findWsChannel(chat.getTargetId()); if (targetChannel != null) { targetChannel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(ResponseBean.ok(chat)))); chat.setIsSelf(2); ResponseBean toRes = ResponseBean.ok(chat); res.setType(2); targetChannel.writeAndFlush(NettyTools.webSocketJson(toRes)); chatProducer.sendMsgHistory(chat); } else { // 在redis中保存用户未在线时,给该用户发送的消息条数 String key = AppContants.MSG_NOTICE + chat.getTo(); String key = AppContants.MSG_NOTICE + chat.getTargetId(); String value = redisUtils.getString(key); if (StrUtil.isEmpty(value)) { redisUtils.set(key , 1); src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java
@@ -41,33 +41,33 @@ log.info("收到历史消息处理:{}", content); ChatRequest chat = JSONObject.parseObject(content, ChatRequest.class); Long toId = Long.parseLong(chat.getTo()); Long fromId = Long.parseLong(chat.getFrom()); Long toId = chat.getTargetId(); Long fromId = chat.getFromMemberId(); // 发送人是否存在聊天框 OtcMsgUserListEntity fromList = otcMsgUserListDao.selectChatListByToAndFrom(Long.parseLong(chat.getTo()), Long.parseLong(chat.getFrom())); OtcMsgUserListEntity fromList = otcMsgUserListDao.selectChatListByToAndFrom(toId, fromId); if (fromList == null) { OtcMsgUserListEntity from = new OtcMsgUserListEntity(); from.setMemberId(Long.parseLong(chat.getFrom())); from.setTargetId(Long.parseLong(chat.getTo())); from.setMemberId(toId); from.setTargetId(fromId); from.setIsRead(OtcMsgUserListEntity.ISREAD_TWO); from.setLastMsgTime(new Date()); otcMsgUserListDao.insert(from); } // 收件人是否存在聊天框 OtcMsgUserListEntity toList = otcMsgUserListDao.selectChatListByToAndFrom(Long.parseLong(chat.getFrom()), Long.parseLong(chat.getTo())); OtcMsgUserListEntity toList = otcMsgUserListDao.selectChatListByToAndFrom(fromId, toId); if (toList == null) { OtcMsgUserListEntity from = new OtcMsgUserListEntity(); from.setMemberId(Long.parseLong(chat.getTo())); from.setTargetId(Long.parseLong(chat.getFrom())); from.setMemberId(toId); from.setTargetId(fromId); from.setIsRead(OtcMsgUserListEntity.ISREAD_ONE); from.setLastMsgTime(new Date()); otcMsgUserListDao.insert(from); } else { // 收件人正在聊的用户 String value = redisUtils.getString(AppContants.MSG_CHATTING + chat.getTo()); if (StrUtil.isNotBlank(value) && value.equals(chat.getFrom())) { String value = redisUtils.getString(AppContants.MSG_CHATTING + toId); if (StrUtil.isNotBlank(value) && value.equals(fromId.toString())) { toList.setLastMsgTime(new Date()); otcMsgUserListDao.updateById(toList); } else { @@ -84,7 +84,7 @@ toHistory.setTargetId(toId); toHistory.setIsSelf(OtcMsgHistoryEntity.ISSELF_TWO); toHistory.setMsgType(chat.getMsgType()); toHistory.setMsg(chat.getContent()); toHistory.setMsg(chat.getMsg()); OtcMsgHistoryEntity fromHistory = new OtcMsgHistoryEntity(); BeanUtil.copyProperties(toHistory, fromHistory);