|
@@ -1,7 +1,10 @@
|
|
|
package com.izouma.meta.web;
|
|
package com.izouma.meta.web;
|
|
|
|
|
|
|
|
import com.izouma.meta.config.Constants;
|
|
import com.izouma.meta.config.Constants;
|
|
|
|
|
+import com.izouma.meta.domain.MetaScheduledFuture;
|
|
|
|
|
+import com.izouma.meta.enums.ScheduledFutureType;
|
|
|
import com.izouma.meta.repo.MetaObjectMoveRepo;
|
|
import com.izouma.meta.repo.MetaObjectMoveRepo;
|
|
|
|
|
+import com.izouma.meta.repo.MetaScheduledFutureRepo;
|
|
|
import com.izouma.meta.websocket.MMOWebSocket;
|
|
import com.izouma.meta.websocket.MMOWebSocket;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
@@ -10,17 +13,19 @@ import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.*;
|
|
import java.util.concurrent.*;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
|
@RequestMapping("/mmo/websocket")
|
|
@RequestMapping("/mmo/websocket")
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
|
-@CrossOrigin(origins = {"http://localhost:8081", "https://raex.vip/", "https://test.raex.vip/"})
|
|
|
|
|
|
|
+@CrossOrigin(origins = {"http://localhost:8081", "https://raex.vip/", "https://test.raex.vip/", "https://www.raex.vip/"})
|
|
|
public class MMOWebSocketController {
|
|
public class MMOWebSocketController {
|
|
|
|
|
|
|
|
- private MMOWebSocket mmoWebSocket;
|
|
|
|
|
- private MetaObjectMoveRepo metaObjectMoveRepo;
|
|
|
|
|
|
|
+ private MMOWebSocket mmoWebSocket;
|
|
|
|
|
+ private MetaObjectMoveRepo metaObjectMoveRepo;
|
|
|
|
|
+ private MetaScheduledFutureRepo metaScheduledFutureRepo;
|
|
|
|
|
|
|
|
private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
|
|
private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
@@ -32,6 +37,14 @@ public class MMOWebSocketController {
|
|
|
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
|
|
|
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> sendMessage(objectId), Constants.META_ROBOT_SEND_MSG_DELAY_MS, Constants.META_ROBOT_SEND_MSG_PERIOD_MS, TimeUnit.MILLISECONDS);
|
|
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> sendMessage(objectId), Constants.META_ROBOT_SEND_MSG_DELAY_MS, Constants.META_ROBOT_SEND_MSG_PERIOD_MS, TimeUnit.MILLISECONDS);
|
|
|
tasks.put(objectId, future);
|
|
tasks.put(objectId, future);
|
|
|
|
|
+ // 保存任务信息
|
|
|
|
|
+ MetaScheduledFuture metaScheduledFuture = metaScheduledFutureRepo.findByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
|
|
|
|
|
+ if (Objects.isNull(metaScheduledFuture)) {
|
|
|
|
|
+ metaScheduledFuture = new MetaScheduledFuture();
|
|
|
|
|
+ metaScheduledFuture.setType(ScheduledFutureType.OBJECT_MOVE);
|
|
|
|
|
+ metaScheduledFuture.setTaskId(objectId);
|
|
|
|
|
+ metaScheduledFutureRepo.save(metaScheduledFuture);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/{objectId}/stop")
|
|
@GetMapping("/{objectId}/stop")
|
|
@@ -41,6 +54,7 @@ public class MMOWebSocketController {
|
|
|
future.cancel(false);
|
|
future.cancel(false);
|
|
|
tasks.remove(objectId);
|
|
tasks.remove(objectId);
|
|
|
}
|
|
}
|
|
|
|
|
+ metaScheduledFutureRepo.deleteByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void sendMessage(String objectId) {
|
|
private void sendMessage(String objectId) {
|