feat: 实现WebSocket通信框架及任务管理功能
新增WebSocket客户端和服务端通信框架,包括会话管理、心跳检测和自动重连机制 添加任务管理器用于处理WebSocket任务创建和执行 实现消息回调处理和错误处理机制 重构销售类型服务并添加缓存支持 移除旧的销售类型服务实现
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.ecep.contract;
|
||||
|
||||
import com.ecep.contract.constant.WebSocketConstant;
|
||||
import com.ecep.contract.task.Tasker;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class WebSocketSession {
|
||||
@Getter
|
||||
private String sessionId = UUID.randomUUID().toString();
|
||||
|
||||
private WebSocketClientTasker tasker;
|
||||
|
||||
private final WebSocketService webSocketService;
|
||||
|
||||
public WebSocketSession(WebSocketService webSocketService) {
|
||||
this.webSocketService = webSocketService;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
}
|
||||
|
||||
public void submitTask(WebSocketClientTasker tasker, Object... args) throws JsonProcessingException {
|
||||
Map<String, Object> argments = Map.of(
|
||||
WebSocketConstant.SESSION_ID_FIELD_NAME, getSessionId(),
|
||||
"type", "createTask",
|
||||
"taskName", tasker.getTaskName(),
|
||||
"args", args);
|
||||
webSocketService.send(argments);
|
||||
}
|
||||
|
||||
public void onMessage(JsonNode node) {
|
||||
if (node.has("type")) {
|
||||
String type = node.get("type").asText();
|
||||
if (type.equals("message")) {
|
||||
JsonNode args = node.get("args");
|
||||
String message = args.get(1).asText();
|
||||
String level = args.get(0).asText();
|
||||
if (tasker instanceof Tasker<?> t) {
|
||||
t.updateMessage(java.util.logging.Level.parse(level), message);
|
||||
}
|
||||
} else if (type.equals("title")) {
|
||||
JsonNode args = node.get("args");
|
||||
String message = args.get(0).asText();
|
||||
if (tasker instanceof Tasker<?> t) {
|
||||
t.updateTitle(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user