ipv4.army/src/components/heart/index.tsx

68 lines
No EOL
1.6 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState } from 'preact/hooks';
import './index.css';
export default () => {
const [heartrate, setHeartrate] = useState(0);
const ws = new WebSocket("wss://app.hyperate.io/socket/websocket?token=wv39nM6iyrNJulvpmMQrimYPIXy2dVrYRjkuHpbRapKT2VSh65ngDGHdCdCtmEN9");
let hrTimeout: ReturnType<typeof setTimeout>;
const setHrInterval = () => {
hrTimeout = setTimeout(() => {
return setHeartrate(0);
}, 6000);
};
ws.onopen = () => {
ws.send(
JSON.stringify({
topic: "hr:0BCA",
event: "phx_join",
payload: {},
ref: 0,
}),
);
setInterval(() => {
ws.send(
JSON.stringify({
topic: "phoenix",
event: "heartbeat",
payload: {},
ref: 0,
}),
);
}, 10000);
return setHrInterval();
};
ws.onmessage = ({ data }) => {
const { event, payload } = JSON.parse(data);
switch (event) {
case "hr_update": {
clearTimeout(hrTimeout);
setHrInterval();
setHeartrate(payload.hr);
break;
}
default: {
break;
}
}
};
return (
<>
<div style={heartrate === 0 ? "display:none" : `--bpm: ${heartrate};`} class="heart">
<br />
<span>{heartrate} BPM</span>
</div>
</>
)
}