sunkean 2 năm trước cách đây
mục cha
commit
cfd6621137

+ 43 - 0
src/main/java/com/izouma/meta/domain/MetaObjectMove.java

@@ -0,0 +1,43 @@
+package com.izouma.meta.domain;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+import java.time.LocalDateTime;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+@ApiModel("元宇宙物体移动配置")
+public class MetaObjectMove extends BaseEntity {
+
+    @ApiModelProperty("物体id")
+    @ExcelProperty("物体id")
+    private Long objectId;
+
+    @ApiModelProperty("物体开始移动时间")
+    @ExcelProperty("物体开始移动时间")
+    private LocalDateTime startTime;
+
+    @ApiModelProperty("总移动时间")
+    @ExcelProperty("总移动时间")
+    private Long totalTime;
+
+    @ApiModelProperty("是否运行")
+    @ExcelProperty("是否运行")
+    private boolean run;
+
+    @ApiModelProperty("区域id")
+    @ExcelProperty("区域id")
+    private Long regionId;
+
+    @ApiModelProperty("城市id")
+    @ExcelProperty("城市id")
+    private Long cityId;
+}

+ 11 - 0
src/main/java/com/izouma/meta/repo/MetaObjectMoveRepo.java

@@ -0,0 +1,11 @@
+package com.izouma.meta.repo;
+
+import com.izouma.meta.domain.MetaObjectMove;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+import java.util.List;
+
+public interface MetaObjectMoveRepo extends JpaRepository<MetaObjectMove, Long>, JpaSpecificationExecutor<MetaObjectMove> {
+    List<MetaObjectMove> findAllByDelAndRun(boolean del, boolean run);
+}

+ 38 - 0
src/main/java/com/izouma/meta/service/WebSocketClient.java

@@ -0,0 +1,38 @@
+package com.izouma.meta.service;
+
+import javax.websocket.ClientEndpoint;
+import javax.websocket.ContainerProvider;
+import javax.websocket.Session;
+import javax.websocket.WebSocketContainer;
+import java.net.URI;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+@ClientEndpoint
+public class WebSocketClient {
+    private Session                  session;
+
+    public WebSocketClient(URI uri) {
+        try {
+            WebSocketContainer container = ContainerProvider.getWebSocketContainer();
+            container.connectToServer(this, uri);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void startSendingMessages(String message, long intervalSeconds) {
+        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
+        executor.scheduleAtFixedRate(() -> sendMessage(message), 0, intervalSeconds, TimeUnit.MILLISECONDS);
+    }
+
+    private void sendMessage(String message) {
+        try {
+            session.getBasicRemote().sendText(message);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+}

+ 19 - 0
src/main/java/com/izouma/meta/web/WebSocketController.java

@@ -0,0 +1,19 @@
+package com.izouma.meta.web;
+
+import com.izouma.meta.service.WebSocketClient;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.net.URI;
+
+@RestController
+@RequestMapping("/webSocketController")
+public class WebSocketController {
+
+    @GetMapping("/run")
+    public void test() {
+        WebSocketClient client = new WebSocketClient(URI.create("ws://test.mmo.raex.vip/websocket/mmo/metaRobot/7970191"));
+        client.startSendingMessages("{type:9}", 1000);
+    }
+}

+ 36 - 23
src/main/java/com/izouma/meta/websocket/MMOWebSocket.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.izouma.meta.config.Constants;
 import com.izouma.meta.domain.MetaMMOLoginInfo;
+import com.izouma.meta.domain.MetaObjectMove;
 import com.izouma.meta.domain.MetaObjectMoveCoordinate;
 import com.izouma.meta.dto.MMOMessage;
 import com.izouma.meta.dto.MMOSingleMessage;
@@ -11,6 +12,7 @@ import com.izouma.meta.dto.MetaRestResult;
 import com.izouma.meta.dto.MetaServiceResult;
 import com.izouma.meta.enums.MoveType;
 import com.izouma.meta.repo.MetaMMOLoginInfoRepo;
+import com.izouma.meta.repo.MetaObjectMoveRepo;
 import com.izouma.meta.utils.ApplicationContextUtil;
 import com.izouma.meta.utils.ObjUtils;
 import lombok.extern.slf4j.Slf4j;
@@ -43,6 +45,7 @@ public class MMOWebSocket {
     private RedisTemplate        redisTemplate;
     private MetaMMOLoginInfoRepo metaMMOLoginInfoRepo;
     private WebsocketCommon      websocketCommon;
+    private MetaObjectMoveRepo   metaObjectMoveRepo;
 
     private void init() {
         if (Objects.isNull(redisTemplate)) {
@@ -54,6 +57,9 @@ public class MMOWebSocket {
         if (Objects.isNull(websocketCommon)) {
             websocketCommon = (WebsocketCommon) ApplicationContextUtil.getBean("websocketCommon");
         }
+        if (Objects.isNull(metaObjectMoveRepo)) {
+            metaObjectMoveRepo = (MetaObjectMoveRepo) ApplicationContextUtil.getBean("metaObjectMoveRepo");
+        }
     }
 
     @OnOpen
@@ -153,27 +159,37 @@ public class MMOWebSocket {
         }
         log.info("来自客户端消息:" + message + "客户端的id是:" + session.getId());
         int type = Integer.parseInt(jsonObject.getString("type"));
-        String cityId = jsonObject.getString("cityId");
-        String regionId = jsonObject.getString("regionId");
-        String key = cityId.concat(":").concat(regionId);
-        List<String> otherUserIds = remove(key, userId);
         if (type == 9) {
-            String objectId = jsonObject.getString("objectId");
-            MetaObjectMoveCoordinate metaObjectMoveCoordinate = null;
-            try {
-                String str = websocketCommon.getRequest("/metaObjectMove/".concat(objectId).concat("/queryCoordinate"));
-                metaObjectMoveCoordinate = JSONObject.parseObject(str, MetaObjectMoveCoordinate.class);
-            } catch (Exception e) {
-                String errMsg = String.format("MetaObjectMoveCoordinate JSON parse throw exception:[%S]", e);
-                log.error(errMsg);
+            List<MetaObjectMove> metaObjectMoves = metaObjectMoveRepo.findAllByDelAndRun(false, true);
+            if (CollectionUtils.isEmpty(metaObjectMoves)) {
+                return;
             }
-            if (Objects.isNull(metaObjectMoveCoordinate)) {
-                log.error(String.format("物体[%S]坐标不存在", objectId));
+            for (MetaObjectMove metaObjectMove : metaObjectMoves) {
+                String cityId = String.valueOf(metaObjectMove.getCityId());
+                String regionId = String.valueOf(metaObjectMove.getRegionId());
+                String key = cityId.concat(":").concat(regionId);
+                List<String> otherUserIds = remove(key, userId);
+                MetaObjectMoveCoordinate metaObjectMoveCoordinate = null;
+                try {
+                    String str = websocketCommon.getRequest("/metaObjectMove/".concat(String.valueOf(metaObjectMove.getObjectId())).concat("/queryCoordinate"));
+                    metaObjectMoveCoordinate = JSONObject.parseObject(str, MetaObjectMoveCoordinate.class);
+                } catch (Exception e) {
+                    String errMsg = String.format("MetaObjectMoveCoordinate JSON parse throw exception:[%S]", e);
+                    log.error(errMsg);
+                }
+                if (Objects.isNull(metaObjectMoveCoordinate)) {
+                    log.error(String.format("物体[%S]坐标不存在", metaObjectMove.getObjectId()));
+                    return;
+                }
+                buildMessageForSendingToAllOther(otherUserIds, 9, metaObjectMoveCoordinate);
                 return;
             }
-            buildMessageForSendingToAllOther(otherUserIds, 9, metaObjectMoveCoordinate);
-            return;
+            ;
         }
+        String cityId = jsonObject.getString("cityId");
+        String regionId = jsonObject.getString("regionId");
+        String key = cityId.concat(":").concat(regionId);
+        List<String> otherUserIds = remove(key, userId);
         MetaMMOLoginInfo metaMMOLoginInfo;
         List<MetaMMOLoginInfo> metaMMOLoginInfos = redisTemplate.opsForValue().multiGet(otherUserIds);
         switch (type) {
@@ -414,19 +430,16 @@ public class MMOWebSocket {
         if (Objects.isNull(jsonObject.getString("type"))) {
             return MetaServiceResult.returnError("Illegal parameter : type can not be null");
         }
+        int type = Integer.parseInt(jsonObject.getString("type"));
+        if (type == 9) {
+            return MetaServiceResult.returnSuccess("check success");
+        }
         if (Objects.isNull(jsonObject.getString("cityId"))) {
             return MetaServiceResult.returnError("Illegal parameter : cityId can not be null");
         }
         if (Objects.isNull(jsonObject.getString("regionId"))) {
             return MetaServiceResult.returnError("Illegal parameter : regionId can not be null");
         }
-        int type = Integer.parseInt(jsonObject.getString("type"));
-        if (type == 9) {
-            if (Objects.isNull(jsonObject.getString("objectId"))) {
-                return MetaServiceResult.returnError("Illegal parameter : objectId can not be null");
-            }
-            return MetaServiceResult.returnSuccess("check success");
-        }
         if (Objects.isNull(jsonObject.getString("id"))) {
             return MetaServiceResult.returnError("Illegal parameter : id can not be null");
         }