@kne/fastify-message 1.0.8 → 1.0.10
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/index.js +3 -1
- package/libs/controllers/main.js +9 -4
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -17,7 +17,9 @@ module.exports = fp(async function(fastify, options) {
|
|
|
17
17
|
}
|
|
18
18
|
}, options);
|
|
19
19
|
|
|
20
|
-
fastify.
|
|
20
|
+
if (!fastify.sse) {
|
|
21
|
+
fastify.register(require('@fastify/sse'));
|
|
22
|
+
}
|
|
21
23
|
|
|
22
24
|
fastify.register(require('@kne/fastify-namespace'), {
|
|
23
25
|
name: options.name,
|
package/libs/controllers/main.js
CHANGED
|
@@ -257,14 +257,19 @@ module.exports = fp(async (fastify, options) => {
|
|
|
257
257
|
}
|
|
258
258
|
},
|
|
259
259
|
async function (request, reply) {
|
|
260
|
-
const intervalSeconds = request.query.interval;
|
|
261
|
-
const { timezone } = request.query;
|
|
262
|
-
|
|
260
|
+
const intervalSeconds = request.query.interval ?? 5;
|
|
261
|
+
const { timezone, type, runnerType } = request.query;
|
|
262
|
+
const startTime = Date.now();
|
|
263
|
+
const maxDurationMs = 30 * 60 * 1000;
|
|
263
264
|
|
|
264
265
|
async function* eventStream() {
|
|
265
266
|
while (reply.sse.isConnected) {
|
|
267
|
+
if (Date.now() - startTime >= maxDurationMs) {
|
|
268
|
+
yield { event: 'timeout', data: JSON.stringify({ message: '连接已超过30分钟,自动断开' }) };
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
266
271
|
try {
|
|
267
|
-
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone })) };
|
|
272
|
+
yield { data: JSON.stringify(await services.statistics.getRealtime({ timezone, type, runnerType })) };
|
|
268
273
|
} catch (err) {
|
|
269
274
|
yield { event: 'error', data: JSON.stringify({ message: err.message }) };
|
|
270
275
|
}
|