refactor(websocket): 优化 WebSocket 消息处理逻辑
- 修改 Operational 类中的 selectOperational 方法,使其直接接受 JSONObject 参数 - 在 SocketServer 类中增加对 ping消息的特殊处理逻辑 - 新增 SocketUtils 类的 isNoPingMessage 方法,用于判断消息是否为 ping - 优化 WebcamShowTask 类中的异常日志输出,避免重复打印相同错误 -调整 logback.xml 中的日志文件总大小限制,从20GB 修改为 200MB
This commit is contained in:
parent
5a4a0dae15
commit
4c77747763
|
@ -21,15 +21,11 @@ public class Operational {
|
||||||
|
|
||||||
private static JSONObject settings = new JSONObject();
|
private static JSONObject settings = new JSONObject();
|
||||||
|
|
||||||
public static String selectOperational(String path) {
|
public static String selectOperational(JSONObject json) {
|
||||||
JSONObject json = JSONObject.parse(path);
|
|
||||||
|
|
||||||
|
log.info("用户发送了数据:\n {}", json.toJSONString());
|
||||||
//操作符
|
//操作符
|
||||||
String function = json.getString("function");
|
String function = json.getString("function");
|
||||||
String type = json.getString("type");
|
|
||||||
if("ping".equals(type)){
|
|
||||||
return R.ok().toJsonStr();
|
|
||||||
}
|
|
||||||
if(function == null){
|
if(function == null){
|
||||||
return R.fail("操作符为空").toJsonStr();
|
return R.fail("操作符为空").toJsonStr();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.aohe.web;
|
package org.aohe.web;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.aohe.result.R;
|
import org.aohe.result.R;
|
||||||
import org.java_websocket.WebSocket;
|
import org.java_websocket.WebSocket;
|
||||||
|
@ -40,8 +41,12 @@ public class SocketServer extends WebSocketServer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(WebSocket conn, String message) {
|
public void onMessage(WebSocket conn, String message) {
|
||||||
log.info("用户发送了数据:\n {}", message);
|
JSONObject json = SocketUtils.isNoPingMessage(message);
|
||||||
conn.send(selectOperational(message));
|
if (json == null){
|
||||||
|
conn.send(R.ok().toJsonStr());
|
||||||
|
}else{
|
||||||
|
conn.send(selectOperational(json));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.aohe.web;
|
package org.aohe.web;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import org.aohe.constant.SocketEnum;
|
import org.aohe.constant.SocketEnum;
|
||||||
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
@ -70,4 +71,17 @@ public class SocketUtils {
|
||||||
map.forEach((k,v)-> safeStop(k));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,9 @@ import java.util.concurrent.TimeUnit;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WebcamShowTask extends Thread {
|
public class WebcamShowTask extends Thread {
|
||||||
|
|
||||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||||
|
|
||||||
|
private String lastLog = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
@ -49,11 +51,15 @@ public class WebcamShowTask extends Thread {
|
||||||
baos.close();
|
baos.close();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
if(!lastLog.equals(e.getMessage())){
|
||||||
|
//相同的错误只打印一次
|
||||||
|
log.error(e.getMessage());
|
||||||
|
lastLog = e.getMessage();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, 0, 100, TimeUnit.MILLISECONDS);
|
}, 0, 100, TimeUnit.MILLISECONDS);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.currentThread().join();
|
Thread.currentThread().join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
log.error("Main thread interrupted", e);
|
log.error("Main thread interrupted", e);
|
||||||
|
@ -65,7 +71,7 @@ public class WebcamShowTask extends Thread {
|
||||||
// SocketUtils.start(SocketEnum.CAM_SOCKET);
|
// SocketUtils.start(SocketEnum.CAM_SOCKET);
|
||||||
// WebCamUtils.setWebcam(0);
|
// WebCamUtils.setWebcam(0);
|
||||||
// WebCamUtils.open();
|
// 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){
|
// while (true){
|
||||||
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
|
// SocketUtils.get(SocketEnum.CAM_SOCKET.getName()).broadcastNew(WebCamUtils.getImageByteBuffer());
|
||||||
// }
|
// }
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
<fileNamePattern>${log.path}/ScanService.%d{yyyy-MM-dd}.log</fileNamePattern>
|
<fileNamePattern>${log.path}/ScanService.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||||
<!-- 日志最大的历史 60天 -->
|
<!-- 日志最大的历史 60天 -->
|
||||||
<maxHistory>60</maxHistory>
|
<maxHistory>60</maxHistory>
|
||||||
<totalSizeCap>20GB</totalSizeCap>
|
<totalSizeCap>200M</totalSizeCap>
|
||||||
</rollingPolicy>
|
</rollingPolicy>
|
||||||
<encoder>
|
<encoder>
|
||||||
<pattern>${log.pattern}</pattern>
|
<pattern>${log.pattern}</pattern>
|
||||||
|
|
Loading…
Reference in New Issue