Pārlūkot izejas kodu

优化socket方法

xiewenjie 3 gadi atpakaļ
vecāks
revīzija
4f2da7871a

+ 26 - 26
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/actionSocket.java

@@ -25,7 +25,7 @@ import java.util.concurrent.ConcurrentHashMap;
  */
 @ServerEndpoint(path = "/wss/action/{token}", port = "${ws.port}", corsOrigins = "*", corsAllowCredentials = "true")
 @Slf4j
-public class actionSocket {
+public class ActionSocket {
 
     @Value("${main.url}")
     private String mainUrl;
@@ -39,22 +39,22 @@ public class actionSocket {
         resData.set("msg", "验证成功");
         session.setSubprotocols("stomp");
         String url = mainUrl + "api/sso/user/checkToken";
-        actionSocket.log.info("验证token的url:{}", url);
+        ActionSocket.log.info("验证token的url:{}", url);
         Map<String, String> headersPrm = new HashMap<>();
         headersPrm.put("token", token);
         com.alibaba.fastjson.JSONObject res = OkHttpUtils.httpPostJson(url, "", headersPrm);
         if (!token.equals("xx-xx-xx-token")) {
             if (null != res) {
-                actionSocket.log.info("登录态校验接口返回:{}", res.toJSONString());
+                ActionSocket.log.info("登录态校验接口返回:{}", res.toJSONString());
                 int code = -10;
                 if (res.containsKey("code")) {
                     code = (int) res.get("code");
                     if (code != 0) {
-                        actionSocket.log.info("Authentication failed!");
+                        ActionSocket.log.info("Authentication failed!");
                         session.close();
                     }
                 } else {
-                    actionSocket.log.info("Authentication failed!");
+                    ActionSocket.log.info("Authentication failed!");
                     session.close();
                 }
             } else {
@@ -65,15 +65,15 @@ public class actionSocket {
 
     @OnOpen
     public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
-        actionSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
+        ActionSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
     }
 
     public void handleSessionMap(Session session) {
-        actionSocket.socketSessionBizMap.keySet().stream().forEach(
+        ActionSocket.socketSessionBizMap.keySet().stream().forEach(
                 key -> {
                     if (key.contains(session.id().toString())) {
-                        actionSocket.socketSessionBizMap.get(key).interrupt();
-                        actionSocket.socketSessionBizMap.remove(key);
+                        ActionSocket.socketSessionBizMap.get(key).interrupt();
+                        ActionSocket.socketSessionBizMap.remove(key);
                     }
                 }
 
@@ -83,7 +83,7 @@ public class actionSocket {
 
     @OnClose
     public void onClose(Session session) throws IOException, InterruptedException {
-        actionSocket.log.info("one connection closed");
+        ActionSocket.log.info("one connection closed");
         handleSessionMap(session);
     }
 
@@ -95,7 +95,7 @@ public class actionSocket {
 
     @OnMessage
     public void onMessage(Session session, String message) throws InterruptedException {
-        actionSocket.log.info(message);
+        ActionSocket.log.info(message);
         JSONObject res = JSONUtil.createObj();
         JSONObject data = JSONUtil.parseObj(message);
         String action = data.getStr("action");
@@ -107,41 +107,41 @@ public class actionSocket {
                 switch (action) {
                     case "componentBiz":
                         ComponentBizImpl componentBiz = new ComponentBizImpl();
-                        String componentBizCallId = componentBiz.getCallId();
-                        if (!actionSocket.socketSessionBizMap.containsKey("componentBiz-id" + data.getInt("id") + "session" + session.id())) {
+                        String componentBizCallId = componentBiz.getCallId(a -> a.getCallId());
+                        if (!ActionSocket.socketSessionBizMap.containsKey("componentBiz-id" + data.getInt("id") + "session" + session.id())) {
                             Thread componentBizTodo = componentBiz.todo(session, message, componentBizCallId);
-                            actionSocket.socketSessionBizMap.put("componentBiz-id" + data.getInt("id") + "session" + session.id(), componentBizTodo);
+                            ActionSocket.socketSessionBizMap.put("componentBiz-id" + data.getInt("id") + "session" + session.id(), componentBizTodo);
                             componentBizTodo.start();
                             componentBiz.listenResult(this::sendMessage, componentBizCallId, new ParamContext("session", session));
                         }
                         break;
                     case "modelUploadBiz":
                         ModelUploadBizImpl modelUploadBiz = new ModelUploadBizImpl();
-                        String modelUploadBizCallId = modelUploadBiz.getCallId();
-                        if (!actionSocket.socketSessionBizMap.containsKey("modelUploadBiz-id" + data.getInt("id") + "session" + session.id())) {
+                        String modelUploadBizCallId = modelUploadBiz.getCallId(a -> a.getCallId());
+                        if (!ActionSocket.socketSessionBizMap.containsKey("modelUploadBiz-id" + data.getInt("id") + "session" + session.id())) {
                             Thread modelUploadBizTodo = modelUploadBiz.todo(session, message, modelUploadBizCallId);
-                            actionSocket.socketSessionBizMap.put("modelUploadBiz-id" + data.getInt("id") + "session" + session.id(), modelUploadBizTodo);
+                            ActionSocket.socketSessionBizMap.put("modelUploadBiz-id" + data.getInt("id") + "session" + session.id(), modelUploadBizTodo);
                             modelUploadBizTodo.start();
                             modelUploadBiz.listenResult(this::sendMessage, modelUploadBizCallId, new ParamContext("session", session));
 
                         }
                         break;
                     case "createImgBiz":
-                        if (!actionSocket.socketSessionBizMap.containsKey("createImgBiz-id" + data.getInt("id") + "session" + session.id())) {
+                        if (!ActionSocket.socketSessionBizMap.containsKey("createImgBiz-id" + data.getInt("id") + "session" + session.id())) {
                             CreateImgBizImpl createImgBiz = new CreateImgBizImpl();
-                            String createImgBizCallId = createImgBiz.getCallId();
+                            String createImgBizCallId = createImgBiz.getCallId(a -> a.getCallId());
                             Thread createImgBizTodo = createImgBiz.todo(session, message, createImgBizCallId);
-                            actionSocket.socketSessionBizMap.put("createImgBiz-id" + data.getInt("id") + "session" + session.id(), createImgBizTodo);
+                            ActionSocket.socketSessionBizMap.put("createImgBiz-id" + data.getInt("id") + "session" + session.id(), createImgBizTodo);
                             createImgBizTodo.start();
                             createImgBiz.listenResult(this::sendMessage, createImgBizCallId, new ParamContext("session", session));
                         }
                         break;
                     case "componentCreateImgBiz":
-                        if (!actionSocket.socketSessionBizMap.containsKey("componentCreateImgBiz-id" + data.getInt("id") + "session" + session.id())) {
+                        if (!ActionSocket.socketSessionBizMap.containsKey("componentCreateImgBiz-id" + data.getInt("id") + "session" + session.id())) {
                             ComponentCreateImgBizImpl componentCreateImgBiz = new ComponentCreateImgBizImpl();
-                            String componentCreateImgBizCallId = componentCreateImgBiz.getCallId();
+                            String componentCreateImgBizCallId = componentCreateImgBiz.getCallId(a -> a.getCallId());
                             Thread componentCreateImgBizTodo = componentCreateImgBiz.todo(session, message, componentCreateImgBizCallId);
-                            actionSocket.socketSessionBizMap.put("componentCreateImgBiz-id" + data.getInt("id") + "session" + session.id(), componentCreateImgBizTodo);
+                            ActionSocket.socketSessionBizMap.put("componentCreateImgBiz-id" + data.getInt("id") + "session" + session.id(), componentCreateImgBizTodo);
                             componentCreateImgBizTodo.start();
                             componentCreateImgBiz.listenResult(this::sendMessage, componentCreateImgBizCallId, new ParamContext("session", session));
                         }
@@ -183,13 +183,13 @@ public class actionSocket {
             IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
             switch (idleStateEvent.state()) {
                 case READER_IDLE:
-                    actionSocket.log.info("read idle");
+                    ActionSocket.log.info("read idle");
                     break;
                 case WRITER_IDLE:
-                    actionSocket.log.info("write idle");
+                    ActionSocket.log.info("write idle");
                     break;
                 case ALL_IDLE:
-                    actionSocket.log.info("all idle");
+                    ActionSocket.log.info("all idle");
                     break;
                 default:
                     break;

+ 14 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/ActionBiz.java

@@ -0,0 +1,14 @@
+package com.fdkk.sxz.webApi.socket.service;
+
+import com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
+
+/**
+ * @author Xiewj
+ * @date 2021/11/17
+ */
+public interface ActionBiz<T> {
+
+    T getCallId(ListenCall listenCall);
+
+
+}

+ 31 - 0
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/ActionBizImpl.java

@@ -0,0 +1,31 @@
+package com.fdkk.sxz.webApi.socket.service;
+
+import com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
+import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
+import com.fdkk.sxz.webApi.socket.bizRunable.SocketBiz;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author Xiewj
+ * @date 2021/11/17
+ */
+@Slf4j
+@Component
+public class ActionBizImpl {
+    
+    private ListenCall listenCall = new ListenCall();
+
+    public <T extends Object> T getCallId(ActionBiz<T> in) {
+        return in.getCallId(listenCall);
+    }
+
+    public void waitForResult(ParamContext p, String callId) {
+        listenCall.waitForResult(p, callId);
+    }
+
+    public void listenResult(SocketBiz<ParamContext, ParamContext> method, String callId, ParamContext context) {
+        listenCall.listenResult(method, callId, context);
+    }
+
+}

+ 1 - 16
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/ComponentBizImpl.java

@@ -8,9 +8,7 @@ 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 com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
 import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
-import com.fdkk.sxz.webApi.socket.bizRunable.SocketBiz;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,9 +22,8 @@ import java.util.concurrent.TimeUnit;
  */
 @Slf4j
 @Component
-public class ComponentBizImpl {
+public class ComponentBizImpl extends ActionBizImpl {
 
-    private ListenCall listenCall = new ListenCall();
 
     private RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
 
@@ -100,16 +97,4 @@ public class ComponentBizImpl {
         return res.toString();
     }
 
-    public String getCallId() {
-        return listenCall.getCallId();
-    }
-
-    public void waitForResult(ParamContext p, String callId) {
-        listenCall.waitForResult(p, callId);
-    }
-
-    public void listenResult(SocketBiz<ParamContext, ParamContext> method, String callId, ParamContext context) {
-        listenCall.listenResult(method, callId, context);
-    }
-
 }

+ 1 - 15
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/ComponentCreateImgBizImpl.java

@@ -8,9 +8,7 @@ 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 com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
 import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
-import com.fdkk.sxz.webApi.socket.bizRunable.SocketBiz;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,9 +22,8 @@ import java.util.concurrent.TimeUnit;
  */
 @Slf4j
 @Component
-public class ComponentCreateImgBizImpl {
+public class ComponentCreateImgBizImpl extends ActionBizImpl {
 
-    private ListenCall listenCall = new ListenCall();
 
     private RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
 
@@ -100,16 +97,5 @@ public class ComponentCreateImgBizImpl {
         return res.toString();
     }
 
-    public String getCallId() {
-        return listenCall.getCallId();
-    }
-
-    public void waitForResult(ParamContext p, String callId) {
-        listenCall.waitForResult(p, callId);
-    }
-
-    public void listenResult(SocketBiz<ParamContext, ParamContext> method, String callId, ParamContext context) {
-        listenCall.listenResult(method, callId, context);
-    }
 
 }

+ 1 - 15
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/CreateImgBizImpl.java

@@ -8,9 +8,7 @@ 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 com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
 import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
-import com.fdkk.sxz.webApi.socket.bizRunable.SocketBiz;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,9 +22,8 @@ import java.util.concurrent.TimeUnit;
  */
 @Slf4j
 @Component
-public class CreateImgBizImpl {
+public class CreateImgBizImpl extends ActionBizImpl {
 
-    private ListenCall listenCall = new ListenCall();
 
     private RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
 
@@ -100,16 +97,5 @@ public class CreateImgBizImpl {
         return res.toString();
     }
 
-    public String getCallId() {
-        return listenCall.getCallId();
-    }
-
-    public void waitForResult(ParamContext p, String callId) {
-        listenCall.waitForResult(p, callId);
-    }
-
-    public void listenResult(SocketBiz<ParamContext, ParamContext> method, String callId, ParamContext context) {
-        listenCall.listenResult(method, callId, context);
-    }
 
 }

+ 1 - 13
sxz-core/src/main/java/com/fdkk/sxz/webApi/socket/service/ModelUploadBizImpl.java

@@ -10,7 +10,6 @@ import com.fdkk.sxz.util.RedisUtil;
 import com.fdkk.sxz.webApi.service.IModelUploadService;
 import com.fdkk.sxz.webApi.socket.bizRunable.ListenCall;
 import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
-import com.fdkk.sxz.webApi.socket.bizRunable.SocketBiz;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,7 +23,7 @@ import java.util.concurrent.TimeUnit;
  */
 @Slf4j
 @Component
-public class ModelUploadBizImpl {
+public class ModelUploadBizImpl extends ActionBizImpl {
 
     private ListenCall listenCall = new ListenCall();
 
@@ -101,16 +100,5 @@ public class ModelUploadBizImpl {
         return res.toString();
     }
 
-    public String getCallId() {
-        return listenCall.getCallId();
-    }
-
-    public void waitForResult(ParamContext p, String callId) {
-        listenCall.waitForResult(p, callId);
-    }
-
-    public void listenResult(SocketBiz<ParamContext, ParamContext> method, String callId, ParamContext context) {
-        listenCall.listenResult(method, callId, context);
-    }
 
 }