mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 09:36:08 +08:00
core&ui: websocket: add reversed heartbeat
This commit is contained in:
2 files changed
+7
-1
No files matched your search
@@ -385,10 +385,12 @@ export function Connection(
|
||||
for (const { name, target } of h.__subscribe || []) disposables.push(bus.on(name, target.bind(h)));
|
||||
let lastHeartbeat = Date.now();
|
||||
const interval = setInterval(() => {
|
||||
if (Date.now() - lastHeartbeat > 40000) conn.close(4000, 'Heartbeat timeout');
|
||||
if (Date.now() - lastHeartbeat > 80000) conn.close(4000, 'Heartbeat timeout');
|
||||
if (Date.now() - lastHeartbeat > 30000) conn.send('ping');
|
||||
}, 40000);
|
||||
conn.onmessage = (e) => {
|
||||
lastHeartbeat = Date.now();
|
||||
if (e.data === 'pong') return;
|
||||
if (e.data === 'ping') {
|
||||
conn.send('pong');
|
||||
return;
|
||||
|
||||
@@ -26,6 +26,10 @@ export default class Sock {
|
||||
};
|
||||
this.sock.onmessage = (message) => {
|
||||
if (message.data === 'pong') return;
|
||||
if (message.data === 'ping') {
|
||||
this.sock.send('pong');
|
||||
return;
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'production') console.log('Sock.onmessage: ', message);
|
||||
const msg = JSON.parse(message.data);
|
||||
if (msg.error === 'PermissionError' || msg.error === 'PrivilegeError') this.close();
|
||||
|
||||
Reference in new issue
Block a user