|
@@ -1,199 +0,0 @@
|
|
|
-package com.fdkk.sxz.webApi.socketController;
|
|
|
-
|
|
|
-
|
|
|
-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.ComponentModelUploadEntity;
|
|
|
-import com.fdkk.sxz.util.RedisUtil;
|
|
|
-import com.fdkk.sxz.webApi.service.IComponentModelUploadService;
|
|
|
-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;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author XieWj
|
|
|
- */
|
|
|
-@ServerEndpoint(path = "/wss/componentWSS/", port = "${ws.port}")
|
|
|
-@Slf4j
|
|
|
-public class ComponentSocket {
|
|
|
-
|
|
|
- @Value("${main.url}")
|
|
|
- private String mainUrl;
|
|
|
-
|
|
|
-
|
|
|
- private static Map<String, ComponentSocket> componentSocketMap = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private RedisUtil redisUtil;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private IComponentModelUploadService componentModelUploadService;
|
|
|
-
|
|
|
- Thread thread = null;
|
|
|
-
|
|
|
- static boolean isStop = true;
|
|
|
-
|
|
|
-
|
|
|
- @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) {
|
|
|
- log.info("Authentication failed!");
|
|
|
- session.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @OnOpen
|
|
|
- public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
|
|
|
- log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
|
|
|
- }
|
|
|
-
|
|
|
- @OnClose
|
|
|
- public void onClose(Session session) throws IOException, InterruptedException {
|
|
|
- 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);
|
|
|
- log.warn("查询" + session.id() + "|----------" + id);
|
|
|
- if (session.isOpen()) {
|
|
|
- String status = data.getStr("status");
|
|
|
- if (StrUtil.equals(status, "query")) {
|
|
|
- if (redisUtil.hasKey("componentUploadStatus:id:" + id)) {
|
|
|
- String ObjStatus = redisUtil.get("componentUploadStatus:id:" + id);
|
|
|
- if (StrUtil.equals(ObjStatus, "done")) {
|
|
|
- extracted(session, data, id);
|
|
|
- return;
|
|
|
- }
|
|
|
- } else {
|
|
|
- ComponentModelUploadEntity entity = componentModelUploadService.findById(id);
|
|
|
- if (ObjectUtil.isNotNull(entity)) {
|
|
|
- if (entity.getStatus() == 1 && entity.getProgress() == 100) {
|
|
|
- extracted(session, data, id);
|
|
|
- return;
|
|
|
- }
|
|
|
- }else {
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (Thread.currentThread().isInterrupted()) {
|
|
|
- log.info("i has isInterrupted");
|
|
|
- return;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- //连接关闭直接return
|
|
|
- isStop = false;
|
|
|
- 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
|
|
|
- componentSocketMap.remove("id" + data.getInt("id") + "session" + session.id());
|
|
|
- Thread.yield();
|
|
|
- }
|
|
|
-
|
|
|
- @OnMessage
|
|
|
- public void onMessage(Session session, String message) throws InterruptedException {
|
|
|
- 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");
|
|
|
- isStop = true;
|
|
|
- if (!componentSocketMap.containsKey("id" + data.getInt("id") + "session" + session.id())) {
|
|
|
- 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:
|
|
|
- log.info("read idle");
|
|
|
- break;
|
|
|
- case WRITER_IDLE:
|
|
|
- log.info("write idle");
|
|
|
- break;
|
|
|
- case ALL_IDLE:
|
|
|
- log.info("all idle");
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|