8.10 修复日期序列号的bug
This commit is contained in:
parent
d9963f0607
commit
97e4fe2763
@ -25,6 +25,7 @@ import java.util.List;
|
||||
* 图片上传模板
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public abstract class PictureUploadTemplate {
|
||||
|
||||
@Resource
|
||||
|
@ -40,12 +40,19 @@ public class PictureEditHandler extends TextWebSocketHandler {
|
||||
@Lazy
|
||||
private PictureEditEventProducer pictureEditEventProducer;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public PictureEditHandler(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
// 每张图片的编辑状态,key: pictureId, value: 当前正在编辑的用户 ID
|
||||
private final Map<Long, Long> pictureEditingUsers = new ConcurrentHashMap<>();
|
||||
|
||||
// 保存所有连接的会话,key: pictureId, value: 所有正在编辑这张图片的用户会话集合
|
||||
private final Map<Long, Set<WebSocketSession>> pictureSessions = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* 实现连接建立成功后执行的方法,保存会话到集合中,并且给其他会话发送消息
|
||||
*
|
||||
@ -212,18 +219,10 @@ public class PictureEditHandler extends TextWebSocketHandler {
|
||||
private void broadcastToPicture(Long pictureId, PictureEditResponseMessage pictureEditResponseMessage, WebSocketSession excludeSession) throws IOException {
|
||||
Set<WebSocketSession> sessionSet = pictureSessions.get(pictureId);
|
||||
if (CollUtil.isNotEmpty(sessionSet)) {
|
||||
// 创建 ObjectMapper
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
// 配置序列化:将 Long 类型转为 String,解决丢失精度问题
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addSerializer(Long.class, ToStringSerializer.instance);
|
||||
module.addSerializer(Long.TYPE, ToStringSerializer.instance); // 支持 long 基本类型
|
||||
objectMapper.registerModule(module);
|
||||
// 序列化为 JSON 字符串
|
||||
String message = objectMapper.writeValueAsString(pictureEditResponseMessage);
|
||||
TextMessage textMessage = new TextMessage(message);
|
||||
// 使用全局注入的 objectMapper(已经配置 JavaTimeModule 和 Long→String)
|
||||
String json = objectMapper.writeValueAsString(pictureEditResponseMessage);
|
||||
TextMessage textMessage = new TextMessage(json);
|
||||
for (WebSocketSession session : sessionSet) {
|
||||
// 排除掉的 session 不发送 (比如自己发送的广播,自己不接收)
|
||||
if (excludeSession != null && session.equals(excludeSession)) {
|
||||
continue;
|
||||
}
|
||||
|
@ -4,22 +4,22 @@ import com.lmax.disruptor.RingBuffer;
|
||||
import com.lmax.disruptor.dsl.Disruptor;
|
||||
import edu.whut.smilepicturebackend.manager.websocket.model.PictureEditRequestMessage;
|
||||
import edu.whut.smilepicturebackend.model.entity.User;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 图片编辑事件(生产者)
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class PictureEditEventProducer {
|
||||
|
||||
@Resource
|
||||
private Disruptor<PictureEditEvent> pictureEditEventDisruptor;
|
||||
private final Disruptor<PictureEditEvent> pictureEditEventDisruptor;
|
||||
|
||||
/**
|
||||
* 发布事件
|
||||
|
Loading…
x
Reference in New Issue
Block a user