xiewenjie %!s(int64=3) %!d(string=hai) anos
pai
achega
414219bdb4

+ 5 - 0
sxz-core/src/main/java/com/fdkk/sxz/other/listener/RunBuild.java

@@ -32,6 +32,7 @@ import org.springframework.util.CollectionUtils;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Created by Hb_zzZ on 2020/7/3.
@@ -986,6 +987,7 @@ public class RunBuild {
                         modelUploadEntity.setObjPath(objPath.replace(buildPath, ""));
                         modelUploadEntity.setFacesNum(facesNum);
                         modelUploadService.updateById(modelUploadEntity);
+                        redisUtil.setEx("modelUploadStatus:id:" + modelUploadEntity.getId(), "done", 5, TimeUnit.MINUTES);
                         modelHandelStep = new ModelHandelStepEntity();
                         modelHandelStep.setStep(5);
                         modelHandelStep.setStepRemarks("结束");
@@ -1176,6 +1178,7 @@ public class RunBuild {
                         modelUploadEntity.setStatus(1);
                         modelUploadEntity.setProgress(100);
                         modelUploadService.updateById(modelUploadEntity);
+                        redisUtil.setEx("modelUploadStatus:id:" + modelUploadEntity.getId(), "done", 5, TimeUnit.MINUTES);
                         modelHandelStep = new ModelHandelStepEntity();
                         modelHandelStep.setStep(5);
                         modelHandelStep.setStepRemarks("结束");
@@ -1249,6 +1252,8 @@ public class RunBuild {
 //                    componentModelUploadEntity.setImgPath(objPath.replace(buildPath, "").replace(".obj", "") + ".jpg");
                         componentModelUploadEntity.setObjPath(objPath.replace(buildPath, ""));
                         componentModelUploadService.updateById(componentModelUploadEntity);
+                        redisUtil.setEx("componentUploadStatus:id:" + componentModelUploadEntity.getId(), "done", 5, TimeUnit.MINUTES);
+
                         modelHandelStep = new ModelHandelStepEntity();
                         modelHandelStep.setStep(4);
                         modelHandelStep.setStepRemarks("结束");

+ 4 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/ComponentSocket.java

@@ -19,6 +19,7 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author XieWj
@@ -43,6 +44,8 @@ public class ComponentSocket {
 
     static boolean isStop = true;
 
+    private String region = "customWSS";
+
 
     @BeforeHandshake
     public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap) {
@@ -102,6 +105,7 @@ public class ComponentSocket {
                         if (ObjectUtil.isNotNull(entity)) {
                             if (entity.getStatus() == 1 && entity.getProgress() == 100) {
                                 extracted(session, data, id);
+                                redisUtil.setEx("componentUploadStatus:id:" + id, "done", 5, TimeUnit.MINUTES);
                                 return;
                             }
                         } else {

+ 202 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/ModelUploadSocket.java

@@ -0,0 +1,202 @@
+package com.fdkk.sxz.webApi.socket;
+
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.fdkk.sxz.entity.ModelUploadEntity;
+import com.fdkk.sxz.util.RedisUtil;
+import com.fdkk.sxz.webApi.service.IModelUploadService;
+import io.netty.handler.codec.http.HttpHeaders;
+import io.netty.handler.timeout.IdleStateEvent;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.yeauty.annotation.*;
+import org.yeauty.pojo.Session;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author XieWj
+ */
+@ServerEndpoint(path = "/wss/modelUploadWSS/", port = "${ws.port}")
+@Slf4j
+public class ModelUploadSocket {
+
+    @Value("${main.url}")
+    private String mainUrl;
+
+
+    private static Map<String, ModelUploadSocket> componentSocketMap = new ConcurrentHashMap<>();
+
+    @Autowired
+    private RedisUtil redisUtil;
+
+    @Autowired
+    private IModelUploadService modelUploadService;
+
+    Thread thread = null;
+
+    static boolean isStop = true;
+
+    private String region = "customWSS";
+
+
+    @BeforeHandshake
+    public void handshake(Session session, HttpHeaders headers, @PathVariable Map<String, String> pathMap) {
+        String num = pathMap.get("num");
+        String token = pathMap.get("token");
+        Map<String, String> paramsMap = new HashMap();
+        paramsMap.put("num", num);
+        Map<String, String> postHeaders = new HashMap();
+        postHeaders.put("token", token);
+        session.setSubprotocols("stomp");
+//        JSONObject req=OkHttpUtils.httpPostFormReturnJson(mainUrl + "api/scene/isLogin", paramsMap, postHeaders);
+        if (1 == 0) {
+            ModelUploadSocket.log.info("Authentication failed!");
+            session.close();
+        }
+    }
+
+    @OnOpen
+    public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
+        ModelUploadSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
+    }
+
+    @OnClose
+    public void onClose(Session session) throws IOException, InterruptedException {
+        ModelUploadSocket.log.info("one connection closed");
+        if (thread != null) {
+            Thread.sleep(1000);
+            thread.interrupt();
+        }
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) {
+        throwable.printStackTrace();
+        if (thread != null) {
+            thread.interrupt();
+        }
+    }
+
+    public void toDo(Session session, String message) throws InterruptedException {
+        while (true) {
+            JSONObject data = JSONUtil.parseObj(message);
+            Long id = data.getLong("id");
+            Thread.sleep(1000);
+            ModelUploadSocket.log.warn("查询" + session.id() + "|----------" + id);
+            if (session.isOpen()) {
+                String status = data.getStr("status");
+                if (StrUtil.equals(status, "query")) {
+                    if (redisUtil.hasKey("modelUploadStatus:id:" + id)) {
+                        String ObjStatus = redisUtil.get("modelUploadStatus:id:" + id);
+                        if (StrUtil.equals(ObjStatus, "done")) {
+                            extracted(session, data, id);
+                            return;
+                        }
+                    } else {
+                        ModelUploadEntity entity = modelUploadService.findById(id);
+                        if (ObjectUtil.isNotNull(entity)) {
+                            if (entity.getStatus() == 1 && entity.getProgress() == 100) {
+                                extracted(session, data, id);
+                                redisUtil.setEx("modelUploadStatus:id:" + id, "done", 5, TimeUnit.MINUTES);
+                                return;
+                            }
+                        } else {
+                            return;
+                        }
+                    }
+
+                    if (Thread.currentThread().isInterrupted()) {
+                        ModelUploadSocket.log.info("i has isInterrupted");
+                        return;
+                    }
+                }
+            } else {
+                //连接关闭直接return
+                ModelUploadSocket.isStop = false;
+                ModelUploadSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+                return;
+            }
+        }
+    }
+
+    private void extracted(Session session, JSONObject data, Long id) {
+        JSONObject res = JSONUtil.createObj();
+        JSONObject resData = JSONUtil.createObj();
+        resData.set("id", id);
+        resData.set("status", "done");
+        res.set("msg", "done");
+        res.set("data", resData);
+        res.set("code", 0);
+        session.sendText(res.toString());
+        //连接关闭直接return
+        ModelUploadSocket.componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
+        Thread.yield();
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message) throws InterruptedException {
+        ModelUploadSocket.log.info(message);
+        JSONObject res = JSONUtil.createObj();
+        res.set("code", 0);
+        //业务处理 调用异步线程,处理成功返回消息,连接断开,销毁线程
+        if (JSONUtil.isJson(message)) {
+            JSONObject data = JSONUtil.parseObj(message);
+            res.set("msg", "is Json");
+            ModelUploadSocket.isStop = true;
+            if (!ModelUploadSocket.componentSocketMap.containsKey("id" + data.getInt("id") + "session" + session.id())) {
+                ModelUploadSocket.componentSocketMap.put("id" + data.getInt("id") + "session" + session.id(), this);
+                thread = new Thread(() -> {
+                    try {
+                        toDo(session, message);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+
+                });
+                thread.start();
+            }
+        } else {
+            res.set("code", 400);
+            res.set("msg", "Not for Json");
+        }
+        session.sendText(res.toString());
+        session.sendText(session.id().toString());
+    }
+
+
+    @OnBinary
+    public void onBinary(Session session, byte[] bytes) {
+        for (byte b : bytes) {
+            System.out.println(b);
+        }
+        session.sendBinary(bytes);
+    }
+
+    @OnEvent
+    public void onEvent(Session session, Object evt) {
+        if (evt instanceof IdleStateEvent) {
+            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
+            switch (idleStateEvent.state()) {
+                case READER_IDLE:
+                    ModelUploadSocket.log.info("read idle");
+                    break;
+                case WRITER_IDLE:
+                    ModelUploadSocket.log.info("write idle");
+                    break;
+                case ALL_IDLE:
+                    ModelUploadSocket.log.info("all idle");
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}