MMOWebSocketController.java 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.izouma.meta.web;
  2. import com.izouma.meta.config.Constants;
  3. import com.izouma.meta.domain.MetaScheduledFuture;
  4. import com.izouma.meta.enums.ScheduledFutureType;
  5. import com.izouma.meta.repo.MetaObjectMoveRepo;
  6. import com.izouma.meta.repo.MetaScheduledFutureRepo;
  7. import com.izouma.meta.websocket.MMOWebSocket;
  8. import lombok.AllArgsConstructor;
  9. import org.apache.commons.collections.CollectionUtils;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.springframework.web.bind.annotation.*;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Objects;
  15. import java.util.concurrent.*;
  16. import java.util.stream.Collectors;
  17. @RestController
  18. @RequestMapping("/mmo/websocket")
  19. @AllArgsConstructor
  20. @CrossOrigin(origins = {"http://localhost:8081", "https://raex.vip/", "https://test.raex.vip/", "https://www.raex.vip/"})
  21. public class MMOWebSocketController {
  22. private MMOWebSocket mmoWebSocket;
  23. private MetaObjectMoveRepo metaObjectMoveRepo;
  24. private MetaScheduledFutureRepo metaScheduledFutureRepo;
  25. private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
  26. @GetMapping("/{objectId}/start")
  27. public synchronized void startSendingMessages(@PathVariable String objectId) {
  28. if (tasks.containsKey(objectId)) {
  29. throw new IllegalArgumentException(String.format("Task with id %S is already running", objectId));
  30. }
  31. ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
  32. ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> sendMessage(objectId), Constants.META_ROBOT_SEND_MSG_DELAY_MS, Constants.META_ROBOT_SEND_MSG_PERIOD_MS, TimeUnit.MILLISECONDS);
  33. tasks.put(objectId, future);
  34. // 保存任务信息
  35. MetaScheduledFuture metaScheduledFuture = metaScheduledFutureRepo.findByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
  36. if (Objects.isNull(metaScheduledFuture)) {
  37. metaScheduledFuture = new MetaScheduledFuture();
  38. metaScheduledFuture.setType(ScheduledFutureType.OBJECT_MOVE);
  39. metaScheduledFuture.setTaskId(objectId);
  40. metaScheduledFutureRepo.save(metaScheduledFuture);
  41. }
  42. }
  43. @GetMapping("/{objectId}/stop")
  44. public synchronized void stopSendingMessages(@PathVariable String objectId) {
  45. ScheduledFuture<?> future = tasks.get(objectId);
  46. if (future != null) {
  47. future.cancel(false);
  48. tasks.remove(objectId);
  49. }
  50. metaScheduledFutureRepo.deleteByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
  51. }
  52. private void sendMessage(String objectId) {
  53. if (mmoWebSocket != null) {
  54. mmoWebSocket.onMessage(Constants.META_ROBOT_NICK_NAME, Constants.META_ROBOT_USER_ID, String.format(Constants.META_ROBOT_MESSAGE_BODY, objectId), null);
  55. }
  56. }
  57. @GetMapping("/check")
  58. public synchronized String check() {
  59. List<Long> objectId = metaObjectMoveRepo.findAllRunningObjectId();
  60. if (CollectionUtils.isEmpty(objectId)) {
  61. if (tasks.isEmpty()) {
  62. return "success";
  63. }
  64. return String.format("物体: %s 未结束广播坐标任务,请联系管理员处理", String.join(",", tasks.keySet()));
  65. }
  66. if (tasks.isEmpty()) {
  67. return String.format("物体: %s 未开启广播坐标任务,请联系管理员处理", objectId.stream().map(Object::toString).collect(Collectors.toList()));
  68. }
  69. StringBuffer str = new StringBuffer();
  70. objectId.forEach(id -> {
  71. if (!tasks.containsKey(String.valueOf(id))) {
  72. str.append(",").append(id);
  73. }
  74. });
  75. if (StringUtils.isNotBlank(str)) {
  76. return String.format("物体: %s 未开启广播坐标任务,请联系管理员处理", str);
  77. }
  78. return "success";
  79. }
  80. }