feat: rate-limit websocket client-side

This commit is contained in:
Laureηt 2021-12-13 23:45:01 +01:00
parent b06bef3b26
commit 29a363fc2c
No known key found for this signature in database
GPG key ID: D88C6B294FD40994

View file

@ -2,13 +2,20 @@
import { onMount } from 'svelte';
let websocket: WebSocket | undefined;
let last_time: number = new Date().getTime();
onMount(() => {
websocket = new WebSocket('ws://localhost:6789/');
});
const sendAction = (key: string) => () => {
if (websocket) websocket.send(JSON.stringify({ action: key }));
if (websocket) {
let current_time: number = new Date().getTime();
if (current_time - last_time > 500) {
websocket.send(JSON.stringify({ action: key }));
last_time = current_time;
}
}
};
</script>