refactor(websocket): 优化 WebSocket 消息处理逻辑

- 修改 Operational 类中的 selectOperational 方法,使其直接接受 JSONObject 参数
- 在 SocketServer 类中增加对 ping消息的特殊处理逻辑
- 新增 SocketUtils 类的 isNoPingMessage 方法,用于判断消息是否为 ping
- 优化 WebcamShowTask 类中的异常日志输出,避免重复打印相同错误
-调整 logback.xml 中的日志文件总大小限制,从20GB 修改为 200MB
This commit is contained in:
李建国 2025-02-21 14:01:18 +08:00
parent 5a4a0dae15
commit 4c77747763
5 changed files with 34 additions and 13 deletions

View File

@ -21,15 +21,11 @@ public class Operational {
private static JSONObject settings = new JSONObject();
public static String selectOperational(String path) {
JSONObject json = JSONObject.parse(path);
public static String selectOperational(JSONObject json) {
log.info("用户发送了数据:\n {}", json.toJSONString());
//操作符
String function = json.getString("function");
String type = json.getString("type");
if("ping".equals(type)){
return R.ok().toJsonStr();
}
if(function == null){
return R.fail("操作符为空").toJsonStr();
}

View File

@ -1,5 +1,6 @@
package org.aohe.web;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aohe.result.R;
import org.java_websocket.WebSocket;
@ -40,8 +41,12 @@ public class SocketServer extends WebSocketServer {
@Override
public void onMessage(WebSocket conn, String message) {
log.info("用户发送了数据:\n {}", message);
conn.send(selectOperational(message));
JSONObject json = SocketUtils.isNoPingMessage(message);
if (json == null){
conn.send(R.ok().toJsonStr());
}else{
conn.send(selectOperational(json));
}
}
@Override

View File

@ -1,5 +1,6 @@
package org.aohe.web;
import com.alibaba.fastjson2.JSONObject;
import org.aohe.constant.SocketEnum;
import java.net.UnknownHostException;
@ -70,4 +71,17 @@ public class SocketUtils {
map.forEach((k,v)-> safeStop(k));
}
/**
* json 格式是否是ping,不是ping就解析
* @param message
* @return
*/
public static JSONObject isNoPingMessage(String message){
JSONObject json = JSONObject.parse(message);
String type = json.getString("type");
if("ping".equals(type)){
return json;
}
return null;
}
}

View File

@ -16,7 +16,9 @@ import java.util.concurrent.TimeUnit;
@Slf4j
public class WebcamShowTask extends Thread {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private String lastLog = "";
@Override
public void run() {
@ -49,11 +51,15 @@ public class WebcamShowTask extends Thread {
baos.close();
}
} catch (Exception e) {
log.error(e.getMessage(), e);
if(!lastLog.equals(e.getMessage())){
//相同的错误只打印一次
log.error(e.getMessage());
lastLog = e.getMessage();
}
}
}, 0, 100, TimeUnit.MILLISECONDS);
try {
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
log.error("Main thread interrupted", e);
@ -65,7 +71,7 @@ public class WebcamShowTask extends Thread {
// SocketUtils.start(SocketEnum.CAM_SOCKET);
// WebCamUtils.setWebcam(0);
// WebCamUtils.open();
// //File file = new File("\"C:\\Users\\JianGuo\\Pictures\\Screenshots\\abc.png");
// //File file = new File("\"C:\\Users\\xxx\\Pictures\\Screenshots\\abc.png");
// while (true){
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
// }

View File

@ -20,7 +20,7 @@
<fileNamePattern>${log.path}/ScanService.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- 日志最大的历史 60天 -->
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
<totalSizeCap>200M</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${log.pattern}</pattern>