Skip to content

Commit dd91f76

Browse files
refactor: code
1 parent b28bae2 commit dd91f76

File tree

5 files changed

+32
-41
lines changed

5 files changed

+32
-41
lines changed

lib/Server.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,9 +1142,6 @@ class Server {
11421142
}
11431143

11441144
createServer() {
1145-
const https = require("https");
1146-
const http = require("http");
1147-
11481145
if (this.options.https) {
11491146
if (this.options.http2) {
11501147
// TODO: we need to replace spdy with http2 which is an internal module
@@ -1158,18 +1155,22 @@ class Server {
11581155
this.app
11591156
);
11601157
} else {
1158+
const https = require("https");
1159+
11611160
this.server = https.createServer(this.options.https, this.app);
11621161
}
11631162
} else {
1163+
const http = require("http");
1164+
11641165
this.server = http.createServer(this.app);
11651166
}
11661167

11671168
this.server.on("connection", (socket) => {
1168-
// add socket to list
1169+
// Add socket to list
11691170
this.sockets.push(socket);
11701171

11711172
socket.once("close", () => {
1172-
// remove socket from list
1173+
// Remove socket from list
11731174
this.sockets.splice(this.sockets.indexOf(socket), 1);
11741175
});
11751176
});
@@ -1783,40 +1784,42 @@ class Server {
17831784
}
17841785

17851786
async stop() {
1786-
if (this.webSocketProxies.length > 0) {
1787-
this.webSocketProxies = [];
1788-
}
1787+
this.webSocketProxies = [];
17891788

1790-
if (this.staticWatchers.length > 0) {
1791-
await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
1789+
await Promise.all(this.staticWatchers.map((watcher) => watcher.close()));
17921790

1793-
this.staticWatchers = [];
1794-
}
1791+
this.staticWatchers = [];
17951792

17961793
if (this.webSocketServer) {
17971794
await new Promise((resolve) => {
17981795
this.webSocketServer.implementation.close(() => {
1796+
this.webSocketServer = null;
1797+
17991798
resolve();
18001799
});
1801-
});
18021800

1803-
this.webSocketServer = null;
1801+
for (const client of this.webSocketServer.clients) {
1802+
client.terminate();
1803+
}
1804+
1805+
this.webSocketServer.clients = [];
1806+
});
18041807
}
18051808

18061809
if (this.server) {
18071810
await new Promise((resolve) => {
1808-
this.server.kill((cb) => {
1809-
this.server.stopCallback(cb);
1810-
this.sockets.forEach((socket) => {
1811-
socket.destroy();
1812-
});
1813-
// reset so the server can be restarted
1814-
this.sockets = [];
1811+
this.server.close(() => {
1812+
this.server = null;
1813+
18151814
resolve();
18161815
});
1817-
});
18181816

1819-
this.server = null;
1817+
for (const socket of this.sockets) {
1818+
socket.destroy();
1819+
}
1820+
1821+
this.sockets = [];
1822+
});
18201823

18211824
if (this.middleware) {
18221825
await new Promise((resolve, reject) => {

lib/servers/BaseServer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
module.exports = class BaseServer {
66
constructor(server) {
77
this.server = server;
8-
this.clients = new Set();
8+
this.clients = [];
99
}
1010
};

lib/servers/SockJSServer.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,14 @@ module.exports = class SockJSServer extends BaseServer {
6767
client.send = client.write;
6868
client.terminate = client.close;
6969

70-
this.clients.add(client);
70+
this.clients.push(client);
7171

7272
client.on("close", () => {
73-
this.clients.delete(client);
73+
this.clients.splice(this.clients.indexOf(client), 1);
7474
});
7575
});
7676

7777
this.implementation.close = (callback) => {
78-
for (const client of this.clients) {
79-
client.close();
80-
}
81-
82-
this.clients.clear();
83-
8478
callback();
8579
};
8680
}

lib/servers/WebsocketServer.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module.exports = class WebsocketServer extends BaseServer {
5454
}, WebsocketServer.heartbeatInterval);
5555

5656
this.implementation.on("connection", (client) => {
57-
this.clients.add(client);
57+
this.clients.push(client);
5858

5959
client.isAlive = true;
6060

@@ -63,18 +63,12 @@ module.exports = class WebsocketServer extends BaseServer {
6363
});
6464

6565
client.on("close", () => {
66-
this.clients.delete(client);
66+
this.clients.splice(this.clients.indexOf(client), 1);
6767
});
6868
});
6969

7070
this.implementation.on("close", () => {
7171
clearInterval(interval);
72-
73-
for (const ws of this.clients) {
74-
ws.terminate();
75-
}
76-
77-
this.clients.clear();
7872
});
7973
}
8074
};

test/e2e/web-socket-communication.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("web socket communication", () => {
9696
}, 200);
9797
});
9898

99-
expect(server.webSocketServer.clients.size).toBe(0);
99+
expect(server.webSocketServer.clients.length).toBe(0);
100100
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
101101
"console messages"
102102
);

0 commit comments

Comments
 (0)