Skip to content

Commit ef61320

Browse files
committed
Improved session ending logic.
1 parent e031dfd commit ef61320

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/connect/http-routes/handlers/create-command.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { spawn } from '@homebridge/node-pty-prebuilt-multiarch';
2+
import chalk from 'chalk';
23
import { Router } from 'express';
34

45
import { SocketServer } from '../../socket-server.js';
@@ -92,10 +93,17 @@ export function createCommandHandler(command: ConnectCommand): Router {
9293

9394
pty.onExit(({ exitCode, signal }) => {
9495
console.log('pty exit', exitCode, signal);
96+
ws.send(Buffer.from(chalk.blue(`Session ended exit code ${exitCode}`), 'utf8'))
97+
9598
ws.terminate();
9699
server.close();
97100
})
98101

102+
ws.on('close', () => {
103+
console.log('Session ws closed. Shutting down pty');
104+
pty.kill();
105+
manager.removeSession(sessionId)
106+
})
99107

100108
return res.status(204).json({});
101109
});

src/connect/socket-server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export class SocketServer {
5656
)
5757
}
5858

59+
removeSession(id: string) {
60+
this.sessions.delete(id);
61+
}
62+
5963
// Under normal use, there should only be 1 socket (1 connection) per namespace.
6064
getSession(id: string): Session | undefined {
6165
return this.sessions.get(id);
@@ -124,4 +128,4 @@ export class SocketServer {
124128
})
125129
}
126130

127-
}
131+
}

0 commit comments

Comments
 (0)