Skip to content

Commit 69ea424

Browse files
committed
fix: close open http server sockets immediatly
1 parent 9701fe1 commit 69ea424

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"scripts": {
2020
"build": "npm run clean && webpack && npm run build-copy-files",
2121
"clean": "shx rm -rf dist",
22-
"test": "jest --config jest.config.json --coverage --detectOpenHandles",
22+
"test": "jest --config jest.config.json --coverage",
2323
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
2424
"lint": "tslint --project ./tsconfig.json",
2525
"coverage": "cat coverage/lcov.info | coveralls",

src/server.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,21 @@ export class Server {
88
private instance: HttpServer;
99
private plotsContainer: IPlotsContainer = {};
1010
private port: number;
11+
private sockets: {[id: number]: any} = {};
12+
private nextSocketID = 0;
1113

1214
constructor(port: number) {
1315
this.port = port;
1416
this.instance = this.createServer();
17+
18+
this.instance.on('connection', (socket) => {
19+
const id = this.nextSocketID++;
20+
this.sockets[id] = socket;
21+
22+
socket.on('close', () => {
23+
delete this.sockets[id];
24+
});
25+
});
1526
}
1627

1728
/**
@@ -33,6 +44,10 @@ export class Server {
3344
* Closes the webserver and clears the plots container.
3445
*/
3546
public clean() {
47+
for (const socket of Object.values(this.sockets)) {
48+
socket.destroy();
49+
}
50+
3651
if (this.instance.address()) {
3752
this.instance.close();
3853
}
@@ -86,7 +101,7 @@ export class Server {
86101
}
87102

88103
/**
89-
*
104+
* Serves the website at http://localhost:PORT/plots/:id/index.html
90105
* @param req
91106
* @param res
92107
*/

test/server.www.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,4 @@ describe('Server', () => {
7777
server.clean();
7878
server = null;
7979
});
80-
});
80+
});

0 commit comments

Comments
 (0)