refactor: improve shutdown cleanup

This commit is contained in:
Carlos Valente
2026-03-13 22:52:18 +01:00
committed by Carlos Valente
parent 871a6b46c8
commit 4408bdb0d3
5 changed files with 135 additions and 30 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
export interface IAdapter {
shutdown: () => void;
shutdown: () => Promise<void>;
}
+9 -2
View File
@@ -72,10 +72,17 @@ class OscServer implements IAdapter {
});
this.udpSocket.bind(port);
}
shutdown() {
shutdown(): Promise<void> {
logger.info(LogOrigin.Rx, 'OSC: Closing server');
this.udpSocket?.close();
const socket = this.udpSocket;
this.udpSocket = null;
if (!socket) {
return Promise.resolve();
}
return new Promise((resolve) => {
socket.close(() => resolve());
});
}
}
+19 -2
View File
@@ -235,8 +235,25 @@ class SocketServer implements IAdapter {
}
}
shutdown() {
this.wss?.close();
shutdown(): Promise<void> {
const wss = this.wss;
if (!wss) {
return Promise.resolve();
}
return new Promise((resolve) => {
// Notify clients first so they can reconnect gracefully
for (const client of wss.clients) {
if (client.readyState === WebSocket.OPEN || client.readyState === WebSocket.CONNECTING) {
client.close(1001, 'Server shutting down');
}
}
wss.close(() => {
this.wss = null;
resolve();
});
});
}
}