Skip to content

Commit 8f14b07

Browse files
committed
fix: client disconnect and engine stopiing
1 parent 2ea7fcc commit 8f14b07

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/api/WebSocketMessageHandler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void handleMessage(String message) {
4242
return;
4343
}
4444

45+
if (engine != null) {
46+
logger.warn("Simulation is already running. Stopping the current simulation before starting a new one.");
47+
stopEngine();
48+
}
49+
4550
// Get and set the simulation data:
4651
SimulationData data = null;
4752
try {
@@ -57,7 +62,7 @@ public void handleMessage(String message) {
5762
engine.start();
5863
}
5964

60-
private void stopEngine() {
65+
public void stopEngine() {
6166
if (engine != null) {
6267
engine.stop();
6368
}

src/api/WebSocketServer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private void handleRead(SelectionKey key) {
179179
}
180180
}
181181

182-
} catch (IOException e) {
182+
} catch (Exception e) {
183183
logger.warn("Error reading from client", e);
184184
clients.remove(clientChannel);
185185
try {
@@ -188,6 +188,11 @@ private void handleRead(SelectionKey key) {
188188
// Ignore
189189
}
190190
key.cancel();
191+
} finally {
192+
if (clients.isEmpty()) {
193+
logger.info("No clients connected, stopping");
194+
messageHandler.stopEngine();
195+
}
191196
}
192197
}
193198

src/graphics/RenderPanelWeb.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public void drawTransparentRect(int x, int y, int width, int height, Color color
7474
*/
7575
public void update() {
7676
double nowTime = System.nanoTime();
77-
logger.info("Time since last tick: " + (nowTime - lastTick) * 1e-6 + " ms");
77+
if ((nowTime - lastTick) * 1e-6 > 50) {
78+
logger.info("Time since last tick: " + (nowTime - lastTick) * 1e-6 + " ms");
79+
}
7880
lastTick = nowTime;
7981

8082
Gson g = new Gson();

0 commit comments

Comments
 (0)