Files
wanderer/assets/js/hooks/ping.ts
Dmitry Popov 4136aaad76 Initial commit
2024-09-18 01:55:30 +04:00

28 lines
569 B
TypeScript

export default {
_nowMs: Date.now(),
mounted() {
const hook = this;
this.handleEvent('pong', () => {
const rtt = Date.now() - this._nowMs;
hook.el.dataset.tip = `ping: ${rtt}ms`;
setTimeout(() => {
hook.ping(rtt);
}, 1000 * 60);
});
this.ping(null);
},
reconnected() {
this.ping(null);
},
disconnected() {
// this.el.dataset.tip = `ping: No connection`;
// this.el.classList.add('text-red-500');
},
ping(rtt) {
this._nowMs = Date.now();
this.pushEvent('ping', { rtt: rtt });
},
};