mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: websocket: use conn.terminate for timeouts
This commit is contained in:
1 file changed
+16
-8
@@ -384,8 +384,21 @@ export function Connection(
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||||
for (const { name, target } of h.__subscribe || []) disposables.push(bus.on(name, target.bind(h)));
|
for (const { name, target } of h.__subscribe || []) disposables.push(bus.on(name, target.bind(h)));
|
||||||
let lastHeartbeat = Date.now();
|
let lastHeartbeat = Date.now();
|
||||||
const interval = setInterval(() => {
|
let closed = false;
|
||||||
if (Date.now() - lastHeartbeat > 80000) conn.close(4000, 'Heartbeat timeout');
|
let interval: NodeJS.Timer;
|
||||||
|
const clean = () => {
|
||||||
|
if (closed) return;
|
||||||
|
closed = true;
|
||||||
|
bus.emit('connection/close', h);
|
||||||
|
if (interval) clearInterval(interval);
|
||||||
|
disposables.forEach((d) => d());
|
||||||
|
h.cleanup?.(args);
|
||||||
|
};
|
||||||
|
interval = setInterval(() => {
|
||||||
|
if (Date.now() - lastHeartbeat > 80000) {
|
||||||
|
clean();
|
||||||
|
conn.terminate();
|
||||||
|
}
|
||||||
if (Date.now() - lastHeartbeat > 30000) conn.send('ping');
|
if (Date.now() - lastHeartbeat > 30000) conn.send('ping');
|
||||||
}, 40000);
|
}, 40000);
|
||||||
conn.onmessage = (e) => {
|
conn.onmessage = (e) => {
|
||||||
@@ -397,12 +410,7 @@ export function Connection(
|
|||||||
}
|
}
|
||||||
h.message?.(JSON.parse(e.data.toString()));
|
h.message?.(JSON.parse(e.data.toString()));
|
||||||
};
|
};
|
||||||
conn.onclose = () => {
|
conn.onclose = clean;
|
||||||
bus.emit('connection/close', h);
|
|
||||||
clearInterval(interval);
|
|
||||||
disposables.forEach((d) => d());
|
|
||||||
h.cleanup?.(args);
|
|
||||||
};
|
|
||||||
await bus.parallel('connection/active', h);
|
await bus.parallel('connection/active', h);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
await h.onerror(e);
|
await h.onerror(e);
|
||||||
|
|||||||
Reference in new issue
Block a user