feat: rate-limit websocket client-side
This commit is contained in:
parent
b06bef3b26
commit
29a363fc2c
|
@ -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>
|
||||
|
||||
|
|
Loading…
Reference in a new issue