mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 09:46:28 +08:00
core&ui: add connection heartbeat
This commit is contained in:
3 files changed
+24
-3
No files matched your search
@@ -383,9 +383,21 @@ export function Connection(
|
||||
if (h.prepare) await h.prepare(args);
|
||||
// eslint-disable-next-line @typescript-eslint/no-shadow
|
||||
for (const { name, target } of h.__subscribe || []) disposables.push(bus.on(name, target.bind(h)));
|
||||
conn.onmessage = (e) => h.message?.(JSON.parse(e.data.toString()));
|
||||
let lastHeartbeat = Date.now();
|
||||
const interval = setInterval(() => {
|
||||
if (Date.now() - lastHeartbeat > 40000) conn.close(4000, 'Heartbeat timeout');
|
||||
}, 40000);
|
||||
conn.onmessage = (e) => {
|
||||
if (e.data === 'ping') {
|
||||
conn.send('pong');
|
||||
lastHeartbeat = Date.now();
|
||||
return;
|
||||
}
|
||||
h.message?.(JSON.parse(e.data.toString()));
|
||||
};
|
||||
conn.onclose = () => {
|
||||
bus.emit('connection/close', h);
|
||||
clearInterval(interval);
|
||||
disposables.forEach((d) => d());
|
||||
h.cleanup?.(args);
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user