|
@@ -0,0 +1,213 @@
|
|
|
|
+package com.fdkk.sxz.webApi.socketController;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
+import com.fdkk.sxz.entity.ComponentModelUploadEntity;
|
|
|
|
+import com.fdkk.sxz.util.OkHttpUtils;
|
|
|
|
+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.SneakyThrows;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import net.oschina.j2cache.CacheChannel;
|
|
|
|
+import net.oschina.j2cache.CacheObject;
|
|
|
|
+import net.oschina.j2cache.CacheProviderHolder;
|
|
|
|
+import net.oschina.j2cache.Command;
|
|
|
|
+import net.oschina.j2cache.cluster.ClusterPolicy;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
|
+import org.yeauty.annotation.*;
|
|
|
|
+import org.yeauty.pojo.Session;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Properties;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author XieWj
|
|
|
|
+ */
|
|
|
|
+@ServerEndpoint(path = "/componentWSS/", port = "8888")
|
|
|
|
+@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"));
|
|
|
|
+ isStop = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @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();
|
|
|
|
+ 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("componetUploadStatus::id::" + id)) {
|
|
|
|
+ String ObjStatus = redisUtil.get("componetUploadStatus::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 interputed");
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|