SSE on words

This commit is contained in:
2024-12-29 22:06:57 +01:00
parent 29ffc556ec
commit e0ece631d3
8 changed files with 126 additions and 65 deletions
+19 -6
View File
@@ -1,4 +1,6 @@
<script setup lang="ts">
import { subscribe } from "~/netcode";
import { handlers } from "~/netcode/handlers/word.handler";
import { useWordStore } from "~/store/word.store";
const newWord = ref("");
@@ -27,12 +29,23 @@ async function createWord(e: Event) {
}
}
// watch(wordInput, (e) =>
// setInterval(() => {
// // e.focus();
// console.log(wordInput.value);
// }, 1000)
// );
let eventSource: EventSource;
onBeforeMount(() => {
eventSource = subscribe(`/words/subscribe`);
eventSource.addEventListener("message", async (message) => {
const data = JSON.parse(message.data);
const type = data.type as string;
if (type in handlers) {
handlers[type as keyof typeof handlers](data);
}
});
});
onUnmounted(() => {
if (eventSource) {
eventSource.close();
}
});
</script>
<template>