|
@@ -1,166 +0,0 @@
|
|
|
-package com.yhlxj.web.wss;
|
|
|
|
|
-
|
|
|
|
|
-import cn.hutool.json.JSONObject;
|
|
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
|
|
-import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
|
|
-import com.yhlxj.dao.model.entity.MidjourneyUser;
|
|
|
|
|
-import com.yhlxj.dao.model.entity.MidjourneyUserConversation;
|
|
|
|
|
-import com.yhlxj.dao.model.enums.WsMessageTypeEnum;
|
|
|
|
|
-import com.yhlxj.redis.RedisService;
|
|
|
|
|
-import com.yhlxj.service.midjourney.MidjourneyService;
|
|
|
|
|
-import com.yhlxj.service.midjourney.impl.MidjourneyServiceImpl;
|
|
|
|
|
-import com.yhlxj.util.SpringCtxUtils;
|
|
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
|
|
-
|
|
|
|
|
-import javax.websocket.*;
|
|
|
|
|
-import javax.websocket.server.PathParam;
|
|
|
|
|
-import javax.websocket.server.ServerEndpoint;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * @author chan
|
|
|
|
|
- * @date 2024/6/28 13:59
|
|
|
|
|
- */
|
|
|
|
|
-@Component
|
|
|
|
|
-@ServerEndpoint("/ws/midjourney/{userToken}/{taskId}")
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class MidjourneyServerEndpoint {
|
|
|
|
|
- private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
-
|
|
|
|
|
- //webSocket
|
|
|
|
|
- public static Map<Long, WssSession> WSS_SESSION_MAP = new ConcurrentHashMap<>();
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 连接建立回调
|
|
|
|
|
- *
|
|
|
|
|
- * @param session
|
|
|
|
|
- */
|
|
|
|
|
- @OnOpen
|
|
|
|
|
- public void onOpen(Session session, @PathParam("userToken") String userToken, @PathParam("taskId") String taskId) throws Exception {
|
|
|
|
|
- log.info("[WS建立连接],token:{}, taskId:{}", userToken, taskId);
|
|
|
|
|
-
|
|
|
|
|
- session.setMaxIdleTimeout(TimeUnit.MINUTES.toMillis(60));
|
|
|
|
|
- //检查uniodId是否合法
|
|
|
|
|
- if (StringUtils.isBlank(userToken) || StringUtils.isBlank(taskId)) {
|
|
|
|
|
-
|
|
|
|
|
- log.error("[WS建立连接失败],[参数非法],{},{}", userToken, taskId);
|
|
|
|
|
-
|
|
|
|
|
- session.getBasicRemote().sendText("入参错误");
|
|
|
|
|
- session.close();
|
|
|
|
|
- }
|
|
|
|
|
- MidjourneyUser user = getUser(userToken);
|
|
|
|
|
- if (user == null) {
|
|
|
|
|
- log.error("[WS建立连接失败],[用户过期],{},{}", userToken, taskId);
|
|
|
|
|
- session.getBasicRemote().sendText("用户过期");
|
|
|
|
|
- session.close();
|
|
|
|
|
- } else {
|
|
|
|
|
- MidjourneyService midjourneyService = SpringCtxUtils.getBean(MidjourneyServiceImpl.class);
|
|
|
|
|
- MidjourneyUserConversation conversation = midjourneyService.getConversationById(taskId);
|
|
|
|
|
- WssSession wsSession = new WssSession(session);
|
|
|
|
|
- if (conversation == null) {
|
|
|
|
|
- wsSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_NOT_FIND, "任务不存在.");
|
|
|
|
|
- } else {
|
|
|
|
|
- WSS_SESSION_MAP.put(user.getId(), wsSession);
|
|
|
|
|
- wsSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, new JSONObject(conversation));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 收到客户端消息回调
|
|
|
|
|
- *
|
|
|
|
|
- * @param message
|
|
|
|
|
- * @param session
|
|
|
|
|
- */
|
|
|
|
|
- @OnMessage
|
|
|
|
|
- public void OnMessage(String message, Session session, @PathParam("userToken") String userToken, @PathParam("taskId") String taskId) {
|
|
|
|
|
- try {
|
|
|
|
|
- log.info("[WS收到消息],{},{},{}", userToken, taskId, message);
|
|
|
|
|
-
|
|
|
|
|
- WssSession wsSession = new WssSession(session);
|
|
|
|
|
- JSONObject body = null;//消息具体内容
|
|
|
|
|
-
|
|
|
|
|
- //处理消息类型,是轮询还是消息
|
|
|
|
|
- switch (WsMessageTypeEnum.getMessageType(message.split(":")[0])) {
|
|
|
|
|
- //ping pong
|
|
|
|
|
- case PING:
|
|
|
|
|
- wsSession.pong();
|
|
|
|
|
- return;
|
|
|
|
|
- case MESSAGE:
|
|
|
|
|
- body = JSONUtil.parseObj(message.substring(8));
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- log.error("[WS消息类型异常],{},{},{}", userToken, taskId, message);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //根据子类型进行处理
|
|
|
|
|
- switch (WsMessageTypeEnum.getMessageType(body.getStr("type"))) {
|
|
|
|
|
-
|
|
|
|
|
- case CLIENT_TASK_ID: //首次查看任务状态
|
|
|
|
|
- MidjourneyService midjourneyService = SpringCtxUtils.getBean(MidjourneyService.class);
|
|
|
|
|
- midjourneyService.getConversationById(body.getStr("taskId"));
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- case CLIENT_CLOSE:
|
|
|
|
|
- log.info("[WS连接关闭],{},{}", userToken, taskId);
|
|
|
|
|
- //dailyQuizService.close(uniodId);
|
|
|
|
|
- break;
|
|
|
|
|
-
|
|
|
|
|
- default:
|
|
|
|
|
- log.error("[WS消息子类型异常],{},{},{}", userToken, taskId, message);
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- log.error("[WS接收消息失败],{},{},{},{}", userToken, taskId, message, e);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 发生错误回调
|
|
|
|
|
- *
|
|
|
|
|
- * @param session
|
|
|
|
|
- * @param error
|
|
|
|
|
- */
|
|
|
|
|
- @OnError
|
|
|
|
|
- public void OnError(Session session, Throwable error, @PathParam("userToken") String userToken, @PathParam("taskId") String taskId) throws Exception {
|
|
|
|
|
- log.error("[WS ONERROR],{}", error.getMessage());
|
|
|
|
|
- if (session.isOpen()) {
|
|
|
|
|
- session.close();
|
|
|
|
|
- }
|
|
|
|
|
- log.info("[WS ONERROR],移除连接池内连接");
|
|
|
|
|
- MidjourneyUser user = getUser(userToken);
|
|
|
|
|
- WSS_SESSION_MAP.remove(user.getId());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 关闭连接回调
|
|
|
|
|
- *
|
|
|
|
|
- * @param session
|
|
|
|
|
- */
|
|
|
|
|
- @OnClose
|
|
|
|
|
- public void OnClose(Session session, @PathParam("userToken") String userToken, @PathParam("taskId") String taskId) throws Exception {
|
|
|
|
|
-
|
|
|
|
|
- log.info("[WS连接关闭],{},{}", userToken, taskId);
|
|
|
|
|
- if (session.isOpen()) {
|
|
|
|
|
- session.close();
|
|
|
|
|
- }
|
|
|
|
|
- log.info("[WS连接关闭],移除连接池内连接");
|
|
|
|
|
- MidjourneyUser user = getUser(userToken);
|
|
|
|
|
- WSS_SESSION_MAP.remove(user.getId());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public MidjourneyUser getUser(String userToken) throws Exception{
|
|
|
|
|
- MidjourneyService midjourneyService = SpringCtxUtils.getBean(MidjourneyServiceImpl.class);
|
|
|
|
|
- return midjourneyService.getUser(userToken);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|