|
@@ -5,7 +5,6 @@ import io.github.nnkwrik.common.exception.GlobalException;
|
|
import io.github.nnkwrik.common.util.JsonUtil;
|
|
import io.github.nnkwrik.common.util.JsonUtil;
|
|
import io.github.nnkwrik.imservice.dao.ChatMapper;
|
|
import io.github.nnkwrik.imservice.dao.ChatMapper;
|
|
import io.github.nnkwrik.imservice.dao.HistoryMapper;
|
|
import io.github.nnkwrik.imservice.dao.HistoryMapper;
|
|
-import io.github.nnkwrik.imservice.model.po.Chat;
|
|
|
|
import io.github.nnkwrik.imservice.model.po.History;
|
|
import io.github.nnkwrik.imservice.model.po.History;
|
|
import io.github.nnkwrik.imservice.model.po.LastChat;
|
|
import io.github.nnkwrik.imservice.model.po.LastChat;
|
|
import io.github.nnkwrik.imservice.model.vo.WsMessage;
|
|
import io.github.nnkwrik.imservice.model.vo.WsMessage;
|
|
@@ -16,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
@@ -49,20 +49,34 @@ public class WebSocketServiceImpl implements WebSocketService {
|
|
chatEndpoint.sendMessage(senderId, Response.fail(e.getErrno(), e.getErrmsg()));
|
|
chatEndpoint.sendMessage(senderId, Response.fail(e.getErrno(), e.getErrmsg()));
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+ if (!senderId.equals(message.getSenderId())){
|
|
|
|
+ String msg = "发送者与ws连接中的不一致,消息发送失败";
|
|
|
|
+ log.info(msg);
|
|
|
|
+ chatEndpoint.sendMessage(senderId, Response.fail(Response.SENDER_AND_WS_IS_NOT_MATCH, msg));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //更新数据库
|
|
|
|
+ try {
|
|
|
|
+ updateSQL(message);
|
|
|
|
+ updateRedis(message);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ String msg = "添加聊天记录时发生异常,消息发送失败";
|
|
|
|
+ log.info(msg);
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ chatEndpoint.sendMessage(senderId, Response.fail(Response.UPDATE_HISTORY_TO_SQL_FAIL, msg));
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
//如果接收方在线,转发ws消息到接收方
|
|
//如果接收方在线,转发ws消息到接收方
|
|
if (chatEndpoint.hasConnect(message.getReceiverId())) {
|
|
if (chatEndpoint.hasConnect(message.getReceiverId())) {
|
|
chatEndpoint.sendMessage(message.getReceiverId(), Response.ok(message));
|
|
chatEndpoint.sendMessage(message.getReceiverId(), Response.ok(message));
|
|
}
|
|
}
|
|
-
|
|
|
|
- //更新redis
|
|
|
|
- updateRedis(message);
|
|
|
|
- //更新数据库
|
|
|
|
- updateSQL(message);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private void updateRedis(WsMessage message) {
|
|
private void updateRedis(WsMessage message) {
|
|
- LastChat lastChat = redisClient.hget(message.getReceiverId(), message.getGoodsId()+"");
|
|
|
|
|
|
+ LastChat lastChat = redisClient.hget(message.getReceiverId(), message.getGoodsId() + "");
|
|
if (lastChat != null) {
|
|
if (lastChat != null) {
|
|
lastChat.setUnreadCount(lastChat.getUnreadCount() + 1);
|
|
lastChat.setUnreadCount(lastChat.getUnreadCount() + 1);
|
|
lastChat.setLastMsg(message);
|
|
lastChat.setLastMsg(message);
|
|
@@ -71,10 +85,11 @@ public class WebSocketServiceImpl implements WebSocketService {
|
|
lastChat.setUnreadCount(1);
|
|
lastChat.setUnreadCount(1);
|
|
lastChat.setLastMsg(message);
|
|
lastChat.setLastMsg(message);
|
|
}
|
|
}
|
|
- redisClient.hset(message.getReceiverId(), message.getGoodsId()+"", lastChat);
|
|
|
|
|
|
+ redisClient.hset(message.getReceiverId(), message.getGoodsId() + "", lastChat);
|
|
}
|
|
}
|
|
|
|
|
|
- private void updateSQL(WsMessage message) {
|
|
|
|
|
|
+ @Transactional
|
|
|
|
+ public void updateSQL(WsMessage message) throws Exception {
|
|
String u1 = null;
|
|
String u1 = null;
|
|
String u2 = null;
|
|
String u2 = null;
|
|
if (message.getSenderId().compareTo(message.getReceiverId()) > 0) {
|
|
if (message.getSenderId().compareTo(message.getReceiverId()) > 0) {
|
|
@@ -85,22 +100,9 @@ public class WebSocketServiceImpl implements WebSocketService {
|
|
u2 = message.getReceiverId();
|
|
u2 = message.getReceiverId();
|
|
}
|
|
}
|
|
|
|
|
|
- //取chatid
|
|
|
|
- Integer chatId = chatMapper.getChatId(u1, u2, message.getGoodsId());
|
|
|
|
- if (chatId == null) {
|
|
|
|
- Chat chat = new Chat();
|
|
|
|
- chat.setU1(u1);
|
|
|
|
- chat.setU2(u2);
|
|
|
|
- chat.setGoodsId(message.getGoodsId());
|
|
|
|
- chatMapper.addChat(chat);
|
|
|
|
- chatId = chat.getId();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
//添加聊天记录
|
|
//添加聊天记录
|
|
History history = new History();
|
|
History history = new History();
|
|
BeanUtils.copyProperties(message, history);
|
|
BeanUtils.copyProperties(message, history);
|
|
- history.setChatId(chatId);
|
|
|
|
boolean u1ToU2 = u1.equals(message.getSendTime()) ? true : false;
|
|
boolean u1ToU2 = u1.equals(message.getSendTime()) ? true : false;
|
|
history.setU1ToU2(u1ToU2);
|
|
history.setU1ToU2(u1ToU2);
|
|
historyMapper.addHistory(history);
|
|
historyMapper.addHistory(history);
|
|
@@ -113,7 +115,8 @@ public class WebSocketServiceImpl implements WebSocketService {
|
|
log.info(msg);
|
|
log.info(msg);
|
|
throw new GlobalException(Response.MESSAGE_FORMAT_IS_WRONG, msg);
|
|
throw new GlobalException(Response.MESSAGE_FORMAT_IS_WRONG, msg);
|
|
}
|
|
}
|
|
- if (StringUtils.isEmpty(wsMessage.getSenderId()) ||
|
|
|
|
|
|
+ if (ObjectUtils.isEmpty(wsMessage.getChatId()) ||
|
|
|
|
+ StringUtils.isEmpty(wsMessage.getSenderId()) ||
|
|
StringUtils.isEmpty(wsMessage.getReceiverId()) ||
|
|
StringUtils.isEmpty(wsMessage.getReceiverId()) ||
|
|
ObjectUtils.isEmpty(wsMessage.getGoodsId()) ||
|
|
ObjectUtils.isEmpty(wsMessage.getGoodsId()) ||
|
|
ObjectUtils.isEmpty(wsMessage.getMessageType()) ||
|
|
ObjectUtils.isEmpty(wsMessage.getMessageType()) ||
|