crabatool 1.0.364 → 1.0.365
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/server.js +27 -1
- package/package.json +1 -1
package/lib/server.js
CHANGED
|
@@ -10,12 +10,15 @@ exports = module.exports;
|
|
|
10
10
|
|
|
11
11
|
// 每次都重启server
|
|
12
12
|
var aloneServer;
|
|
13
|
+
var connections = new Set();
|
|
14
|
+
|
|
13
15
|
exports.run = function(options) {
|
|
14
16
|
var that = this;
|
|
15
17
|
if (aloneServer) {
|
|
16
|
-
aloneServer
|
|
18
|
+
forceShutdown(aloneServer, function() {
|
|
17
19
|
console.log('关闭旧实例...');
|
|
18
20
|
|
|
21
|
+
connections = new Set();
|
|
19
22
|
that.start(options);
|
|
20
23
|
});
|
|
21
24
|
} else {
|
|
@@ -154,5 +157,28 @@ function createServer(app, options) {
|
|
|
154
157
|
|
|
155
158
|
if (options) {
|
|
156
159
|
aloneServer = server;
|
|
160
|
+
|
|
161
|
+
// 记录所有活跃连接
|
|
162
|
+
server.on('connection', (socket) => {
|
|
163
|
+
connections.add(socket);
|
|
164
|
+
socket.on('close', () => {
|
|
165
|
+
connections.delete(socket);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
|
|
157
170
|
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
// 强制关闭所有活跃连接的函数
|
|
175
|
+
function forceShutdown(server, cb) {
|
|
176
|
+
console.log(`强制关闭 ${connections.size} 个活跃连接`);
|
|
177
|
+
connections.forEach((socket) => {
|
|
178
|
+
socket.destroy(); // 强制销毁 socket
|
|
179
|
+
});
|
|
180
|
+
server.close(() => {
|
|
181
|
+
console.log('服务器已关闭');
|
|
182
|
+
cb();
|
|
183
|
+
});
|
|
158
184
|
}
|