package com.zhujizheng.IHome.chat.net; import com.zhujizheng.IHome.chat.dto.PYSingleChatDTO; import com.zhujizheng.IHome.chat.service.PYSingleChatService; import com.zhujizheng.IHome.chat.vo.PYSingleChatAckVO; import com.zhujizheng.IHome.chat.vo.PYSingleChatVO; import com.zhujizheng.IHome.generator.dao.PYMsgNotify; import com.zhujizheng.IHome.msgnotify.net.NetMsgNotifyAck; import com.zhujizheng.IHome.msgnotify.service.PYMsgNotifyServer; import com.zhujizheng.IHome.msgnotify.vo.PYMsgNotifyVO; import com.zhujizheng.IHome.util.alipush.AliPushService; import com.zhujizheng.IHome.websocket.Net.Base.NetProtocolBase; import com.zhujizheng.IHome.websocket.Net.exception.NetException; import com.zhujizheng.IHome.websocket.server.WebSocketServer; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.concurrent.atomic.AtomicBoolean; @Slf4j @Component public class NetSingleChat extends NetProtocolBase { @Autowired private PYSingleChatService service; @Autowired private AliPushService aliPushService; @Autowired private PYMsgNotifyServer msgNotifyServer; @Override public void dealWithJSONAndSid(JSONObject json, String sid) { super.dealWithJSONAndSid(json, sid); int userId = Integer.parseInt(sid); PYSingleChatDTO dto = (PYSingleChatDTO)JSONObject.toBean(json, PYSingleChatDTO.class); PYSingleChatVO vo = new PYSingleChatVO(); // 设置回调结果的消息状态默认为成功 vo.setStatus(1); vo.setReceiveId(dto.getReceiveId()); // 设置回调结果的消息唯一标识符 vo.setMsgSeq(dto.getMsgSeq()); // 设置消息发送者id dto.setSendId(Integer.parseInt(sid)); // 将消息插入到数据库 PYSingleChatAckVO ackVO = service.pushSingleChatWithDTO(dto); if (null == ackVO) { // 设置回调结果的消息状态为失败 vo.setStatus(0); this.sendObject(vo, sid); return; } WebSocketServer item = WebSocketServer.getWebSocketMap().get(ackVO.getReceiveId().toString()); if (null == item) { // 离线推送 log.info("离线推送::ackVO.getReceiveId().toString() = " + ackVO.getReceiveId().toString()); this.sendMsgNotifyAck(ackVO); aliPushService.pushRemoteNotify("新消息", "您收到一条聊天消息", ackVO.getReceiveId().toString(), ackVO.getSendId().toString(), 1); int result = service.succeedSingleChat(vo.getMsgSeq()); if (result == 0) { log.info("发送状态更新失败"); // 设置回调结果的消息状态为失败 vo.setStatus(0); this.sendObject(vo, sid); } else { // 给自己回调发送的结果,走到这一步才能确保消息发送成功 this.sendObject(vo, sid); } return; } // 给receiveId发送数据 NetSingleChatAck singleChatAck = new NetSingleChatAck(); // 设置即将发送的消息的消息状态为成功 ackVO.setStatus(1); AtomicBoolean sendSuccess = new AtomicBoolean(true); try { this.sendMsgNotifyAck(ackVO); singleChatAck.sendObject(ackVO, ackVO.getReceiveId().toString()); } catch (NetException e) { log.info("发送消息失败"); e.printStackTrace(); sendSuccess.set(false); } finally { if (sendSuccess.get()) { log.info("发送消息成功"); // 更新数据库此消息的状态 int result = service.succeedSingleChat(vo.getMsgSeq()); int offlineResult = service.updateOffline(vo.getMsgSeq()); if (result == 0 || offlineResult == 0) { log.info("发送状态更新失败"); // 设置回调结果的消息状态为失败 vo.setStatus(0); this.sendObject(vo, sid); } else { // 给自己回调发送的结果,走到这一步才能确保消息发送成功 this.sendObject(vo, sid); } } } } private void sendMsgNotifyAck(PYSingleChatAckVO ackVO) { NetMsgNotifyAck ack = new NetMsgNotifyAck(); PYMsgNotify msgNotifyFromReceiver = PYMsgNotify.createChatMsgNotify(ackVO.getReceiveId(), ackVO.getSendId(), ackVO.getMsgContent(), System.currentTimeMillis()); PYMsgNotifyVO voFromReceiver = msgNotifyServer.addOrUpdateMsgNotify(msgNotifyFromReceiver); ack.sendObject(voFromReceiver, ackVO.getReceiveId().toString()); // 先给自己添加一条消息,因为可能之前没有消息通知记录 PYMsgNotify msgNotifyFromSender = PYMsgNotify.createChatMsgNotify(ackVO.getSendId(), ackVO.getReceiveId(), ackVO.getMsgContent(), System.currentTimeMillis()); PYMsgNotifyVO voFromSender = msgNotifyServer.addOrUpdateMsgNotify(msgNotifyFromSender); // 然后更新自己消息数为0 PYMsgNotifyVO voFromSenderResult = msgNotifyServer.clearMsgNotify(voFromSender.getNotifyId()); ack.sendObject(voFromSenderResult, ackVO.getSendId().toString()); } }