整理统一发送websocket接口,并将模拟想定推演时间和剩余时间相关数据的发送

This commit is contained in:
wanglei 2025-09-17 14:01:28 +08:00
parent 3629f00104
commit 1946f33be3
9 changed files with 50 additions and 31 deletions

View File

@ -5,13 +5,15 @@
</component>
<component name="ChangeListManager">
<list default="true" id="050b8051-b1ec-42aa-bac3-c1c189a4697d" name="更改" comment="修改线程继承自定义线程相关类错误问题">
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/config/CorsFilter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/redisUtil.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/controller/InferenceController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/InferenceTaskService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/DemandThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskAssignThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TaskThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/TimeSyncThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WasterThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WasterThread.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/service/WebSocketServer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/simulationservice/util/CustomThread.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -128,7 +130,7 @@
<workItem from="1757664586037" duration="3070000" />
<workItem from="1757820561153" duration="249000" />
<workItem from="1757829080725" duration="24604000" />
<workItem from="1757922989495" duration="18673000" />
<workItem from="1757922989495" duration="29564000" />
</task>
<task id="LOCAL-00001" summary="修改配置文件和去掉打包时test模块">
<option name="closed" value="true" />

View File

@ -35,6 +35,12 @@
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.26</version> <!-- 可以去 [Maven中央仓库](https://search.maven.org/search?q=hutool-all) 查最新版本 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>

View File

@ -13,11 +13,7 @@ public class DemandThread extends CustomThread {
if (max < 0) max = 1000;
max--;
String info = String.valueOf(max) + " " + getRoomId();
try {
WebSocketServer.sendInfo(info, "1111");
} catch (IOException e) {
throw new RuntimeException(e);
}
//SendWebSocketMsg();
}
}

View File

@ -14,11 +14,7 @@ public class TaskAssignThread extends CustomThread {
if (max < 0) max = 1000;
max--;
String info = String.valueOf(max) + " " + getRoomId();
try {
WebSocketServer.sendInfo(info, "3333");
} catch (IOException e) {
throw new RuntimeException(e);
}
//SendWebSocketMsg();
}
}

View File

@ -13,11 +13,6 @@ public class TaskThread extends CustomThread {
if (max < 0) max = 1000;
max--;
String info = String.valueOf(max) + " " + getRoomId();
try {
WebSocketServer.sendInfo(info, "2222");
} catch (IOException e) {
throw new RuntimeException(e);
}
SendWebSocketMsg(info, "111");
}
}

View File

@ -4,6 +4,8 @@ import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.simulationservice.util.CustomThread;
import com.simulationservice.util.RedisUtil;
@ -105,10 +107,18 @@ public class TimeSyncThread extends CustomThread {
// 推演的时间键值roomId-runTime
String key = getRoomId() + "-runTime";
//_redis.set(key, strTime);
SpringUtil.getBean(RedisUtil.class).set(key, strTime);
// 房间剩余时间键值roomId-resvTime
key = getRoomId() + "-resvTime";
//_redis.set(key, _resvTime);
SpringUtil.getBean(RedisUtil.class).set(key, _resvTime);
//推送时间到客户端
JSONObject jsonObject = new JSONObject();
jsonObject.putIfAbsent("roomId", getRoomId());
jsonObject.putIfAbsent("runTime", strTime);
jsonObject.putIfAbsent("resvTime", _resvTime);
String jsonString = jsonObject.toJSONString();
SendWebSocketMsg(jsonString, "1111");
}
public void setBeginTime(String beginTime) {

View File

@ -1,7 +1,9 @@
package com.simulationservice.service;
import java.io.IOException;
import java.lang.*;
import com.simulationservice.util.CustomThread;
/**
* 作战分队物资消耗线程用于各个作战单元的资源消耗
*/
@ -13,11 +15,7 @@ public class WasterThread extends CustomThread {
if (max < 0) max = 1000;
max--;
String info = String.valueOf(max) + " " + getRoomId();
try {
WebSocketServer.sendInfo(info, "1111");
} catch (IOException e) {
throw new RuntimeException(e);
}
//SendWebSocketMsg();
}
}

View File

@ -126,14 +126,18 @@ public class WebSocketServer {
/**
* 发送自定义消息
*/
public static void sendInfo(String message, @PathParam("userId") String userId) throws IOException {
public static <Gson> void sendInfo(String message, @PathParam("userId") String userId) throws IOException {
if (!StringUtils.isNotBlank(userId) || !webSocketMap.containsKey(userId)) {
//log.error("用户" + userId + ",不在线!");
return;
}
log.info("发送消息到:" + userId + ",报文:" + message);
webSocketMap.get(userId).sendMessage(message);
//log.info("发送消息到:" + userId + ",报文:" + message);
JSONObject jsonObject = new JSONObject();
jsonObject.putIfAbsent("name", userId);
jsonObject.putIfAbsent("data", message);
String jsonString = jsonObject.toJSONString();
webSocketMap.get(userId).sendMessage(jsonString);
}
public static synchronized int getOnlineCount() {

View File

@ -1,4 +1,7 @@
package com.simulationservice.util;
import com.simulationservice.service.WebSocketServer;
import java.io.IOException;
import java.lang.*;
/**
* 需求消耗线程用于各个作战单元的需求消耗
@ -11,6 +14,7 @@ public abstract class CustomThread extends Thread {
private String roomId = ""; //房间号
public int max = 1000;
public int time = 1000; //快进和快退时间单位毫秒
private RedisUtil _redis;
@Override
public void run() {
@ -66,4 +70,12 @@ public abstract class CustomThread extends Thread {
public void setTime(int time) {
this.time = time;
}
public void SendWebSocketMsg(String msg, String userId) {
try {
WebSocketServer.sendInfo(msg, userId);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}