Compare commits
No commits in common. "128078acaa179e39cf04f04a0424fbc8a95784e4" and "7f1175e9bd3ac5821170e68dd5bdde180a0ebe7a" have entirely different histories.
128078acaa
...
7f1175e9bd
|
@ -14,9 +14,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
import org.springframework.messaging.simp.user.SimpUserRegistry;
|
import org.springframework.messaging.simp.user.SimpUserRegistry;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.TransactionStatus;
|
|
||||||
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
|
|
||||||
import org.springframework.transaction.support.TransactionTemplate;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
@ -39,9 +36,6 @@ public class StompServiceImpl implements StompService {
|
||||||
@Resource
|
@Resource
|
||||||
private ChatRoomMapper chatRoomMapper;
|
private ChatRoomMapper chatRoomMapper;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private TransactionTemplate transactionTemplate;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult sendMessageToUser(UserMessageVo userMessageVo) {
|
public AjaxResult sendMessageToUser(UserMessageVo userMessageVo) {
|
||||||
WebsocketMessageDto websocketMessageDto = new WebsocketMessageDto();
|
WebsocketMessageDto websocketMessageDto = new WebsocketMessageDto();
|
||||||
|
@ -53,24 +47,11 @@ public class StompServiceImpl implements StompService {
|
||||||
}
|
}
|
||||||
//当前用户发送其他人, 发送给指定用户. 否则默认发送给当前发送者
|
//当前用户发送其他人, 发送给指定用户. 否则默认发送给当前发送者
|
||||||
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(), userMessageVo.getContent());
|
messagingTemplate.convertAndSendToUser(userMessageVo.getEmail(), Destination.QUEUE + "/" + userMessageVo.getEmail(), userMessageVo.getContent());
|
||||||
// 接收者和发送者 都不是游客才能存储消息 和修改聊天室信息。后续可改为消息中间件解耦形式。
|
// 接收者和发送者 都不是游客才能存储消息 和修改聊天室信息
|
||||||
if (!TOURIST.equals(userMessageVo.getReceiveUserType()) && !TOURIST.equals(userMessageVo.getSendUserType())){
|
if (!TOURIST.equals(userMessageVo.getReceiveUserType()) && TOURIST.equals(userMessageVo.getSendUserType())){
|
||||||
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
|
Long id = insertMessage(userMessageVo);
|
||||||
@Override
|
websocketMessageDto.setId(id);
|
||||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
updateRoom(userMessageVo);
|
||||||
try {
|
|
||||||
// 插入消息并获取 ID
|
|
||||||
Long id = insertMessage(userMessageVo);
|
|
||||||
websocketMessageDto.setId(id);
|
|
||||||
// 更新房间信息
|
|
||||||
updateRoom(userMessageVo);
|
|
||||||
} catch (Exception e) {
|
|
||||||
// 回滚事务
|
|
||||||
status.setRollbackOnly();
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return AjaxResult.success(websocketMessageDto);
|
return AjaxResult.success(websocketMessageDto);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue