解决会与前端断开Websocket连接的问题

master
lulicheng 8 months ago
parent 1f34057561
commit afd23cdf12

@ -7,6 +7,7 @@ import org.springframework.stereotype.Component;
import javax.websocket.*; import javax.websocket.*;
import javax.websocket.server.ServerEndpoint; import javax.websocket.server.ServerEndpoint;
import java.nio.ByteBuffer;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
@ -20,8 +21,10 @@ public class SimulationWebsocket {
private static ISimulationService simulationService; private static ISimulationService simulationService;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private boolean isSimulationRunning = false; private volatile boolean isSimulationRunning = false;
private ScheduledExecutorService heartbeatScheduler = Executors.newScheduledThreadPool(1);
@Autowired @Autowired
public void setService(ISimulationService simulationService) { public void setService(ISimulationService simulationService) {
@ -36,6 +39,10 @@ public class SimulationWebsocket {
public void OnOpen(Session session) { public void OnOpen(Session session) {
this.session = session; this.session = session;
webSocketSet.add(this); webSocketSet.add(this);
if(!heartbeatScheduler.isShutdown()){
heartbeatScheduler = Executors.newScheduledThreadPool(1);
}
startHeartbeat();
log.info("【websocket消息】有新的连接, 总数:{}", webSocketSet.size()); log.info("【websocket消息】有新的连接, 总数:{}", webSocketSet.size());
} }
@ -46,6 +53,9 @@ public class SimulationWebsocket {
//发送开始仿真的信息 //发送开始仿真的信息
String result = simulationService.getPlantData(message); String result = simulationService.getPlantData(message);
if ("Simulation Started".equals(result)) { if ("Simulation Started".equals(result)) {
if(scheduler.isShutdown()){
scheduler = Executors.newScheduledThreadPool(1);
}
if (!isSimulationRunning) { if (!isSimulationRunning) {
isSimulationRunning = true; isSimulationRunning = true;
log.info("【仿真开始】"); log.info("【仿真开始】");
@ -59,10 +69,12 @@ public class SimulationWebsocket {
if ("1".equals(data) || "1.00".equals(data)) { if ("1".equals(data) || "1.00".equals(data)) {
sendMessage("ending"); sendMessage("ending");
isSimulationRunning = false; isSimulationRunning = false;
scheduler.shutdown();
} }
} }
} catch (Exception e) { } catch (Exception e) {
isSimulationRunning = false; isSimulationRunning = false;
scheduler.shutdown();
} }
}, 0, 10, TimeUnit.SECONDS); }, 0, 10, TimeUnit.SECONDS);
} }
@ -84,7 +96,9 @@ public class SimulationWebsocket {
@OnClose @OnClose
public void onClose() { public void onClose() {
webSocketSet.remove(this); webSocketSet.remove(this);
stopHeartbeat();
log.info("【websocket消息】连接断开, 总数:{}", webSocketSet.size()); log.info("【websocket消息】连接断开, 总数:{}", webSocketSet.size());
} }
@ -101,4 +115,27 @@ public class SimulationWebsocket {
} }
} }
} }
private void startHeartbeat() {
heartbeatScheduler.scheduleAtFixedRate(() -> {
try {
if (session.isOpen()) {
sendMessage("ping");
}
} catch (Exception e) {
log.error("【websocket心跳】发送心跳失败", e);
}
}, 0, 10, TimeUnit.SECONDS);
}
private void stopHeartbeat() {
heartbeatScheduler.shutdown();
try {
if (!heartbeatScheduler.awaitTermination(5, TimeUnit.SECONDS)) {
heartbeatScheduler.shutdownNow();
}
} catch (InterruptedException e) {
heartbeatScheduler.shutdownNow();
}
}
} }

Loading…
Cancel
Save