|
@@ -0,0 +1,165 @@
|
|
|
+package com.fdkk.sxz.webApi.socket;
|
|
|
+
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.fdkk.sxz.util.RedisUtil;
|
|
|
+import com.fdkk.sxz.webApi.service.IComponentModelUploadService;
|
|
|
+import com.fdkk.sxz.webApi.socket.bizRunable.ParamContext;
|
|
|
+import com.fdkk.sxz.webApi.socket.service.ComponentBizImpl;
|
|
|
+import com.fdkk.sxz.webApi.socket.service.ComponentCreateImgBizImpl;
|
|
|
+import com.fdkk.sxz.webApi.socket.service.CreateImgBizImpl;
|
|
|
+import com.fdkk.sxz.webApi.socket.service.ModelUploadBizImpl;
|
|
|
+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.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author XieWj
|
|
|
+ */
|
|
|
+@ServerEndpoint(path = "/wss/action/", port = "${ws.port}")
|
|
|
+@Slf4j
|
|
|
+public class actionSocket {
|
|
|
+
|
|
|
+ @Value("${main.url}")
|
|
|
+ private String mainUrl;
|
|
|
+
|
|
|
+
|
|
|
+ private static Map<String, actionSocket> componentSocketMap = new ConcurrentHashMap<>();
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IComponentModelUploadService componentModelUploadService;
|
|
|
+
|
|
|
+
|
|
|
+ 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 token = pathMap.get("token");
|
|
|
+ session.setSubprotocols("stomp");
|
|
|
+// JSONObject req=OkHttpUtils.httpPostFormReturnJson(mainUrl + "api/scene/isLogin", paramsMap, postHeaders);
|
|
|
+ if (1 == 0) {
|
|
|
+ actionSocket.log.info("Authentication failed!");
|
|
|
+ session.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnOpen
|
|
|
+ public void onOpen(Session session, HttpHeaders headers, @PathVariable Map pathMap) {
|
|
|
+ actionSocket.log.info("new connection, sessionId-{},Host-{}", session.id(), headers.get("Host"));
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClose
|
|
|
+ public void onClose(Session session) throws IOException, InterruptedException {
|
|
|
+ actionSocket.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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnMessage
|
|
|
+ public void onMessage(Session session, String message) throws InterruptedException {
|
|
|
+ actionSocket.log.info(message);
|
|
|
+ JSONObject res = JSONUtil.createObj();
|
|
|
+ JSONObject data = JSONUtil.parseObj(message);
|
|
|
+ String action = data.getStr("action");
|
|
|
+ res.set("code", 0);
|
|
|
+ res.set("action", action);
|
|
|
+ //业务处理 调用异步线程,处理成功返回消息,连接断开,销毁线程
|
|
|
+ if (JSONUtil.isJson(message)) {
|
|
|
+ switch (action) {
|
|
|
+ case "componentBiz":
|
|
|
+ ComponentBizImpl componentBiz = new ComponentBizImpl();
|
|
|
+ String componentBizCallId = componentBiz.getCallId();
|
|
|
+ componentBiz.todo(session, message, componentBizCallId);
|
|
|
+ componentBiz.listenResult(this::sendMessage, componentBizCallId, new ParamContext("session", session));
|
|
|
+ break;
|
|
|
+ case "modelUploadBiz":
|
|
|
+ ModelUploadBizImpl modelUploadBiz = new ModelUploadBizImpl();
|
|
|
+ String modelUploadBizCallId = modelUploadBiz.getCallId();
|
|
|
+ modelUploadBiz.todo(session, message, modelUploadBizCallId);
|
|
|
+ modelUploadBiz.listenResult(this::sendMessage, modelUploadBizCallId, new ParamContext("session", session));
|
|
|
+ break;
|
|
|
+ case "createImgBiz":
|
|
|
+ CreateImgBizImpl createImgBiz = new CreateImgBizImpl();
|
|
|
+ String createImgBizCallId = createImgBiz.getCallId();
|
|
|
+ createImgBiz.todo(session, message, createImgBizCallId);
|
|
|
+ createImgBiz.listenResult(this::sendMessage, createImgBizCallId, new ParamContext("session", session));
|
|
|
+ break;
|
|
|
+ case "componentCreateImgBiz":
|
|
|
+ ComponentCreateImgBizImpl componentCreateImgBiz = new ComponentCreateImgBizImpl();
|
|
|
+ String componentCreateImgBizCallId = componentCreateImgBiz.getCallId();
|
|
|
+ componentCreateImgBiz.todo(session, message, componentCreateImgBizCallId);
|
|
|
+ componentCreateImgBiz.listenResult(this::sendMessage, componentCreateImgBizCallId, new ParamContext("session", session));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ res.set("code", 400);
|
|
|
+ res.set("msg", "Not for Json");
|
|
|
+ res.set("status", "error");
|
|
|
+ session.sendText(res.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void sendMessage(ParamContext paramContext, ParamContext paramContext1) {
|
|
|
+ String msg = paramContext.get("msg");
|
|
|
+ Session session = paramContext1.get("session");
|
|
|
+ session.sendText(msg.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:
|
|
|
+ actionSocket.log.info("read idle");
|
|
|
+ break;
|
|
|
+ case WRITER_IDLE:
|
|
|
+ actionSocket.log.info("write idle");
|
|
|
+ break;
|
|
|
+ case ALL_IDLE:
|
|
|
+ actionSocket.log.info("all idle");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|