Skip to content

Commit b007374

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

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-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: 17 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
/**
@@ -36,6 +47,11 @@ export class Server {
3647
if (this.instance.address()) {
3748
this.instance.close();
3849
}
50+
51+
for (const socket of Object.values(this.sockets)) {
52+
socket.destroy();
53+
}
54+
3955
this.plotsContainer = {};
4056
}
4157

@@ -86,7 +102,7 @@ export class Server {
86102
}
87103

88104
/**
89-
*
105+
* Serves the website at http://localhost:PORT/plots/:id/index.html
90106
* @param req
91107
* @param res
92108
*/

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)