Template
SSE on words
This commit is contained in:
+19
-6
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user