chore: update .gitignore and enhance WebSocket connection handling

* Add .env.development to .gitignore
* Improve handling of existing WebSocket connections by waiting for closure before terminating
This commit is contained in:
Adam Shiervani 2025-04-09 17:10:15 +02:00
parent efce3ebe7b
commit 507ffc06f9
2 changed files with 16 additions and 5 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
.idea
.env
.env
.env.development

View File

@ -85,10 +85,20 @@ async function handleDeviceSocketRequest(
console.log(
`[Device] Device ${device.id} already connected. Terminating existing connection.`,
);
activeConnections.get(device.id)?.[0]?.terminate();
activeConnections.delete(device.id);
// We don't return here, we just delete the existing connection and let it continue and create a new one.
// Why multiple connections are needed, i don't know, but let's cover it.
const [existingDeviceWs] = activeConnections.get(device.id)!;
await new Promise(resolve => {
console.log("[Device] Waiting for existing connection to close...");
existingDeviceWs.on("close", () => {
activeConnections.delete(device.id);
console.log("[Device] Existing connection closed.");
// Now we continue with the new connection
resolve(true);
});
existingDeviceWs.terminate();
});
}
// Complete the WebSocket upgrade